In which case should I use != or !==? - php

I'm currently working on my first (small) PHP project going step by step teaching myself. It's going good so far but I do have a question about the following code.. and which I should use in which case..
equal:
if ($word1 != $word2) {
echo "Your words do not match, please go back and correct this.";
die(); }
identical:
if ($word1 !== $word2) {
echo "Your words do not match, please go back and correct this.";
die(); }
Th code runs fine with both of these but I would still like a detailed explanation as to when use which one, for future references, and to learn.
Thank you!

You can understand the difference between them by looking at Types comparison table in PHP manual.
Main difference is that !== is strict about type of compared values while != is weaker check.

the one will pass the other one will not the frist one cheks only for equal the second one checks and for type of var. The var $word1 is string
the $word2 is a integer
if ($word1 != $word2) {
echo "Your words do not match, please go back and correct this.";
}
//with this if stament will pass the test without echo out nothing.
if ($word1 !== $word2) {
echo "Your words do not match, please go back and correct this.";
} //this one will not pass and will echo out your string
?>

Related

How to check if string exists in variable output?

Here's what I'm running:
echo $checknetworks;
Here's the results of the echo:
Facebook,Twitter,Myspace,Google,Instagram,Pinterest
What I want to do is check to see if the string google is in the results above. I do not want it to be case sensitive because the capitalization changes from time to time.
Basically if google exists in the string, I want to display "FOUND". If it doesn't exist, I want to display "NOT FOUND".
I came across a couple of somewhat similar questions here but none seemed to take capitalization into account.
You need stripos:
stripos — Find the position of the first occurrence of a case-insensitive substring in a string
$checknetworks = "Facebook,Twitter,Myspace,Google,Instagram,Pinterest";
if (stripos($checknetworks, 'Google') === FALSE)
{
echo 'NOT FOUND';
} else
{
echo 'FOUND';
}
Please note that you should compare types as well. I.e. if your string would start with google, stripos will return 0, that would be interpreted as false, unless you make the type comparison with ===
try using strpos:
<?php
$strVar = (string)$myVar;
if (strpos($strVar, "Google")){
echo "Found"
}else{
echo "Not found"
}
?>
EDIT:
You must check if the strpos returns FALSE, and not the position 0.
Use '===':
if (strpos($strVar, "Google") === FALSE){

Why does a second if work when an else doesn't? (PHP)

Can anyone explain why this:
if($xml->$ul !== ""){
echo $xml->$ul;
}
if($xml->$ul == ""){
echo "0";
}
does work, while
if($xml->$ul !== ""){
echo $xml->$ul;
}else{
echo "0";
}
does not work?
Am i missing something?
Short explanation: if the xml contains $ul it will echo its value, if it is not contained it will echo 0. Works perfectly with first code, but second code just echos the values, the else is completely ignored.
I appreciate all answers!
You are not doing the same equality check. In the first example you are first checking using !==, then in the second if you are using ==.
See this answer for an explanation of the difference between === equality and == equality. In short, === not only checks the values are equal, but also that the types of the variables being compared are the same as well.

storing something if the statement is true

This is a simple code I can't figure out
if(preg_match( "/where/","where's my backpack") == true){
echo "this is a match";
// this is the part i cant figure out
// how can I store it as == $match1
// I tried $match1 == "this is a match"; but it didn't work
}else{
//$match1= " "; << this what I tried and didn't work
//when I say echo match1 later, it displays this is a match even when its wrong
}
how can I store it as a variable or something for later. for example, if those two matches are true, store it as $match1 so that I can later say 'echo $match1' so that it displays "this is a match". if not just display a space.
the overall code is in php BTW
== operator checks for equality, to store something you need to use =
$match1 = '';
if(preg_match("/where/", "where's my backpack"))
$match1 = "this is a match";
else
$match1 = " ";
echo $match1;
You can assign it directly:
$match = preg_match("/where/","where's my backpack") === 1
Now you have a boolean value that you can use anywhere after this statement.
Note that I have replaced your true with 1 as that is what preg_match will return but either will work.
By the way, I realize that this is not exactly what you want but I find it easier to maintain and more reusable when I separate output texts from logic like this.

Php test empty string

I have a bit of php code that I'm not understanding why it is acting as it is. I have a variable called contactId that I want to test to see if it is empty. However even if it is empty it evaluates to true. Code is below. Thanks in advance.
print "*".$contactId."*<br/>";
if($contactId != '')
{
//queryContact($contactId);
print "Contact Present<br/>";
}
result returned to screen is:
**
Contact Present
If you want to see exactly what your string is, simply use var_dump(), like this, for instance:
var_dump($contactId)
instead of
print "*".$contactId."*<br/>";
Couple of things you can try:
if (!empty($contactId)) {
// I have a contact Id
}
// Or
if (strlen($contactId) > 0) {
// I have a contact id
}
In my experience I have often used the latter of the two solutions because there have been instances where I would expect a variable to have the value of 0, which is valid in some contexts. For example, if I have a drink search site and want to indicate if an ingredient is non-alcoholic I would assign it a value of 0 (i.e. IngredientId = 7, Alcoholic = 0).
Do it with if (isset($contactId)) {}.
You likely want:
if (strlen($contactId))
You'll want to learn the difference between '' and null, and between == and ===. See here: http://php.net/manual/en/language.operators.comparison.php
and here: http://us3.php.net/manual/en/language.types.null.php
In future, use if(!empty($str)) { echo "string is not empty"}.

Best way to write an or is equal statement

Hello $mostamazingforumforfastanswersever.
I have a quick silly question; what is the best way to write this :
if ($curpageurl == "www.mysite.com/this" || "www.mysite.com/this/")
{
echo 'this is the this page';
}
and if it isn't, then I need to call
while (isset($somevariable)
{
echo '$somevariable';
}
and if that variable isn't set and we are not on this page, then
else
{
echo 'we are not on this page and the variable isn't set';
}
I know I'm not far from the right answer, and this actually works as is but only if I remove the || this/ portion of my first if statement. Is there a better way to write the or is equal to portion? || == for example? Thanks!
if ($curpageurl == "www.mysite.com/this" || $curpageurl == "www.mysite.com/this/")
If you are only doing this because of the /, maybe the simplest way would be just use substr or something like this to just get the part you want.
So check if the last char is /, if so get the $curpageurl to the last char -1
I am only suggesting this in case you have more possible values for $curpageurl

Categories