Reputation: 21
I am having trouble assigning my if statement to a variable. I am new to PHP, and I can't seem to assign my if statment. The varible keeps being displayed as empty. Have i written this wrong:
if ($url="http://test.co.uk") {
$ifStmt == "strpos($link,'en/bruk/category') && strpos($link, 'store-locator')==false " .
" && strpos($link, 'accessibility')==false && strpos($link, 'terms-conditions')==false " .
" && strpos($link, 'site-map')==false && strpos($link, 'write-a-review')==false " .
" && strpos($link, 'press')==false && strpos($link, 'burton-menswear-apps')==false " .
" && strpos($link, 'black-friday')==false && strpos($link, 'military-discount')==false " .
" && strpos($link, 'gift-card')==false && strpos($link, 'student-discount')==false " .
" && strpos($link, 'formal-hire')==false && strpos($link, 'information-pages')==false " .
" && strpos($link, 'responsibilities')==false && strpos($link, 'affiliates')==false " .
" && strpos($link, 'featurearchive')==false";
}else{
$ifStmt == "strpos($link,'en/bruk/category') && strpos($link, 'evans-on-the-go')==false " .
" && strpos($link, 'CatalogNavigationSearchResultCmd')==false && strpos($link, 'store-locator')==false " .
" && strpos($link, 'LogonForm')==false && strpos($link, 'evans-card')==false " .
" && strpos($link, 'delivery-landing-page')==false && strpos($link, 'accessibility')==false " .
" && strpos($link, 'newsletter-sign-up')==false && strpos($link, 'evans-on-the-go')==false " .
" && strpos($link, 'evans-shape-hub')==false && strpos($link, 'new-lower-price')==false " .
" && strpos($link, 'hpwk35')==false";
}
echo "if statement is ".$ifStmt;
Upvotes: 0
Views: 64
Reputation:
if ($url=="http://test.co.uk") {
$ifStmt =
This is where you are wrong .You need to take a look at assignment operators first.
Two of the many comparison operators used by PHP are '==' (i.e. equal) and '===' (i.e. identical). The difference between the two is that '==' should be used to check if the values of the two operands are equal or not. On the other hand, '===' checks the values as well as the type of operands.
Let me explain more using some examples:
'==' (Equal):
if("22" == 22) echo "YES";
else echo "NO";
The code above will print "YES". The reason is that the values of the operands are equal. Whereas when we run the example code below: '===' (Identical):
if("22" === 22) echo "YES";
else echo "NO";
The result we get is "NO". The reason is that although values of both operands are same their types are different, "22" (with quotes) is a string while 22 (w/o quotes) is an integer. But if we change the code above to the following:
if("22" === (string)22) echo "YES";
else echo "NO";
Then, the result will be "YES". Notice that we changed the type of right operand to a string which is the same as the left operand (i.e. string). Now, the types and values of both left and right operands are the same hence both operands are identical. In a nutshell, whenever you want to compare the values as well as the types of operands you'll use '===' otherwise you use '=='. Whereas '=' means simply assigning the right part to your left part.
$url="Right_part";
Upvotes: 0
Reputation: 98991
change this:
if ($url="http://test.co.uk") {
$ifStmt ==
to this:
if ($url === "http://test.co.uk") {
$ifStmt =
$b = "Hello"; // string variable `$b` now has the value Hello
$a == $b; // Equal TRUE if $a is equal to $b after type juggling.
$a === $b; // Identical TRUE if $a is equal to $b, and they are of the same type.
The rest of your code is unknown to me but functions (strpos($link,'en/bruk/category'))
shouldn't be inside of quotes or they'll be treated as strings.
Upvotes: 2
Reputation: 12391
change your code to
if ($url=="http://test.co.uk") {
$ifStmt = "strpos($link,
need to use ==
in comparison
and ==
for assignment
you used assignment instead of == and used comparison instead of assignment.
I hope you know the difference between ==
and ===
Upvotes: 0
Reputation: 218971
You've confused assignment (=
) with comparison (==
).
Here you aren't actually comparing the values:
if ($url="http://test.co.uk")
You're assigning the string to the variable. (And assigning will always evaluate to true
, so this if
statement will always be true
, but that's beside the point.) So this if
statement isn't actually testing if anything is true, it's just assigning a variable.
Then you compare these:
$ifStmt == "strp...
For starters, this will fail if that variable is never first defined. Because there's nothing to compare. But more to the point, this comparison doesn't do anything. It tests if two things are equal, but doesn't do anything with the result of that test.
Thus, nothing is ever assigned to $ifStmt
. So this won't output anything:
echo "if statement is ".$ifStmt;
Conclusion: Swap out your =
and ==
usages. One equal sign assigns to a variable, two equal signs compare variables.
Upvotes: 0