Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a simple IF always return true?

Tags:

php

Why does the following code always return true?

<?php
$v = "dav6d";
if($v = "david") {
echo "smith";
}
?>
like image 844
mr.smith Avatar asked Feb 13 '26 11:02

mr.smith


1 Answers

This line:

if($v = "david") {

is using an assignment (i.e. a single = sign) which will return the result of $v, "david", which is a truthy value. If you want to do a comparison use == or ===


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!