Can you make an assignment on conditional statement in php as so:
if(siteName_err = isValid("sitename", $_POST['sitename'], false))
{
$siteName = $_POST['sitename'];
}
Using the assignment operator in conditional expressions frequently indicates programmer error and can result in unexpected behavior. The assignment operator should not be used in the following contexts: if (controlling expression)
Conditional statements are used to perform different actions based on different conditions.
Yes, you can assign the value of variable inside if.
Yes.
I think the most common use scenario for this is when using MySQL. For example:
$result = mysql_query("SELECT username FROM user");
while ($user = mysql_fetch_assoc($result)) {
echo $user['username'] . "\n";
}
This works because $user
is the result from the assignment. Meaning, whatever is stored in your assignment, is then used as the conditional. In other words,
var_dump($i = 5);
// is equivalent to
$i = 5;
var_dump($i);
Both will print int(5)
, obviously.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With