Use if condition for assignment – php

Leave a comment

June 8, 2012 by sekar

You can use if condition as an assignment operator. for example, look at the following code:

<?php
function mul($no){
 return ($no>0 ? ($no*$no) : false);
}

$m="";
if($m=mul(5))
 echo "value of m:$m";
else
 echo "m has no value";
?>

If the function mul returns true then the value is assigned to $m. It seems very simple and many of you knew this, may be it will help the beginners… That’s it;)

Leave a comment