This is the code
$a = 'Rs 15.25';
if ( $a != '' && $a! = 0 ) {
echo "Inside If";
} else {
echo "Outside If";
}
actually I want to Print "Inside If" so that's why I put $a='Some String Value'. But it always prints "Outside If". Then I changed my code to
$a = 'Rs 15.25';
if ( $a != '' && $a != '0' ) {
echo "Inside If";
} else {
echo "Outside If";
}
I have just added single quotes to 0. Then i got the exact output as i want. But I didn't understand why this happens.
So please help me with this.
PHP does weak type comparison, that is, it converts both operands to the same type before doing the actual comparison.
If one of the operands is a number, the other one is converted to a number as well. If the second operand is a string and contains no digits, it is silently converted to the number 0.
To avoid this whole issue, use string type checking with the operator !== (=== for equality).
if($a !== '' && $a !== 0) {
echo "Inside If";
} else {
echo "Outside If";
}
First of all you when you have multiple conditions on an if statement you should always enclose each of them within brackets
So first thing you should do is to change your code to
$a='Rs 15.25';
if(($a!='') && ($a!='0'))
{
echo "Inside If";
}else
{
echo "Outside If";
}
In PHP 0 = FALSE, 1 = TRUE.
if($a != 0) -> if($a != FALSE)
if $a = 'Rs 15.25', $a != false and $a not empty, then you have echo "Outside If";
Related
Similar to this question here which was intended for javascript, it has spawned off numerous spin-offs for various different languages. I'm curious if the following can ever evaluate to true in PHP:
($a == 1 && $a == 2 && $a == 3)
To follow up a bit more, it seems simply setting $a = true will yield the desired result (This was not the case for javascript, due to the way type casting works in both languages). A few answers (in javascript) worked with === as well, so in PHP with typechecking (===), can the following ever yield true?
($a === 1 && $a === 2 && $a === 3)
I just tried this:
$a = true;
echo ($a == 1 && $a == 2 && $a == 3);
and it echoed 1.
Because of the type casting and not type checking, 1, 2, 3 will be treated as true when compared to a boolean value.
Answer to the edit: No it can't be done.
Hackish method which #FrankerZ commented about:
Zero byte character = 0xFEFF
http://shapecatcher.com/unicode/info/65279
http://www.unicodemap.org/details/0xFEFF/index.html
$var = "1";
$var = "2";
$ var = "3";
echo ($var === "1" && $var === "2" && $ var === "3") ? "true" : "false";
This code runs with this character because the name $ var and $var seems to be valid for the PHP compiler and with the appropiate font, it can be hidden. It can be achieved with Alt + 65279 on Windows.
Whilst not strictly in keeping with the question, this can be done if the ints are wrapped in quotes:
<?php
class A {
private static $i = 1;
public function __toString()
{
return (string)self::$i++;
}
}
$a = new A();
if($a == '1' && $a == '2' && $a == '3') {
echo 'yep';
} else {
echo 'nope';
}
I can't think of a case where strict comparison would ever yield true. === operator compares the types first, so there's no way to use any magic method wizardry.
For curiosity the closest i could get is to slightly modify the setting and hack the variable in a tick function. Since ticks are only incremeted per statement, we have to break the comparison to multiple statements for this to work.
$a = 1;
register_tick_function(function () use (&$a) {
++$a;
});
declare(ticks = 1) {
$a === 1 or exit(1);
$a === 2 or exit(1);
$a === 3 or exit(1);
}
echo "a = $a\n";
Try it online.
When the user introduces a letter and clicks on submit the script shall return a name that starts with that letter, I wrote it using a Switch/Case structure, now I need to write it using if/elseif/else.
The problem is that no matter what letter I introduce on the Textbox I'll always get the return for A (Aberdefia - Anacleto)
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombradorIf = 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombradorIf = 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
The code for the letters C to Z is just like the one for A and B.
You need == for comparison, = is for assignment, and === is for identical or same type.
Also, you used $nombrador when assigning and $nombradorIf when comparing, resulting in an undefined variable.
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Thus, $nombrador == 'A' means $nombrador is equal to A.
And, $nombrador = 'A' means to assign A to $nombrador.
More information at http://php.net/manual/en/language.operators.comparison.php.
Hope this helps, thanks!
== is the conditional operator = is assigning operator
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Try this code
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} else if($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Your code error near . you condition check variable name is $nombradorIf and also not declare variable but You assign the value variable name is $nombrador and you not use conditional operator == . so result not proper . You use above the code working fine
For checking conditional statement you have to use == (double equal sign) like below:
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombrador == 'A') {
// ^^ Use like this
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
// ^^ Use like this
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
I have $first and $second. They can have value 0 or 1.
if ( $first AND $second ) {
// True
} else {
// False
}
My mind (and Google search) tells me, that the result is true only when $first == 1 and $second == 0 or vice versa. But the result is true when both of this variables are 1.
I don't understand how does it works.
Your Google searches have failed you. PHP's type juggling means that a 1 is equivalent to TRUE and 0 is equivalent to FALSE. (See also type comparisons). So if both values are 1 then that if statement evaluates to TRUE. If one or both values are 0, it evaluates to FALSE.
<?php
$one = 1;
$zero = 0;
if ($one && $one) {
echo "true\n";
}
else {
echo "false\n";
}
if ($zero && $zero) {
echo "true\n";
}
else {
echo "false\n";
}
if ($one && $zero) {
echo "true\n";
}
else {
echo "false\n";
}
if ($zero && $one) {
echo "true\n";
}
else {
echo "false\n";
}
Program Output
true
false
false
false
Demo
In PHP all values are either "truthy" or "falsy" in expressions.
If a value contains something then it can be said to be truthy. So, values such as 1, "one", [1,2,3] or true all "contain" something and will be interpreted as truthy.
Values that are zeroed or in some way empty are falsy. E.g. 0, "", [] and false.
There is a table of how values are interpreted in the PHP documentation.
You can also just experiment, and output it to your website:
var_dump(1 and 0);
I am running into a funny problem with a mischievous "if" condition :
$condition1="53==56";
$condition2="53==57";
$condition3="53==58";
$condition=$condition1."||".$condition2."||".$condition3;
if($condition)
{
echo "blah";
}
else
{
echo "foo";
}
Why does the if condition pass?
Why does php echo "blah"? What do I do to make php evaluate the "if" statement and print "foo"?
The problem here is that you're putting your expressions in strings!
Your $condition1, $condition2, and $condition3 variables contain strings, and not the result of an expression, and the same goes for your $condition variable which will be a string that looks like 53==56||53==57||53==58. When PHP evaluates a string it considers it true if it is not empty and not equal to 0, so your script will output blah.
To fix this you just need to take your expressions out of the strings. It should look like this:
$condition1 = 53 == 56; // false
$condition2 = 53 == 57; // false
$condition3 = 53 == 58; // false
$condition = $condition1 || $condition2 || $condition3; // false || false || false = false
if ($condition) {
echo 'blah';
} else {
echo 'foo'; // This will be output
}
You're evaluating strings as booleans; they'll aways be true (except the strings "" and "0". Get rid of almost all of the quotes in your program.
Those aren't conditions, they're strings.
$condition1=53==56;
$condition2=53==57;
$condition3=53==58;
$condition=$condition1 || $condition2 || $condition3;
if($condition)
{
echo "blah";
}
else
{
echo "foo";
}
Because you're not checking those variables, it's saying if (String) will always return true. (unless "")
You should be doing:
if(53==56 || 53==57 || 53==58)
{
echo "blah";
}
else
{
echo "foo";
}
All $condition* variables will evaluate to true. This is how PHP sees it:
if("53==56" || "53==57" || "53==58")
What you want is this:
$condition1 = 53==56;
$condition2 = 53==57;
$condition3 = 53==58;
It's because you're evaluating a string, and strings other than empty strings evaluate to true.
You are concatting a string together, a non-empty string equals TRUE in php.
Because when the if passes, $condition is a string (a concatenation of) containing the text of your conditions. Try using if(eval($condition)).
String always evaluate to true if its not empty
And btw php make implicit conversion to boolean
if(0 == ('Pictures'))
{
echo 'true';
}
why it's giving me 'true' ?
Your string will be evaluated as an Integer, so becomes 0, use this : 0 === 'Pictures' that verifies identity (same value and same type)
Check PHP type comparison tables to understand how comparison operators behave in PHP.
In your case, 'Pictures' becomes "0" and therefore 0 = 0.
Let's check following example:
echo (int)'Pictures'; // 0 => 'Picture' as int
echo 0 == 'Pictures'; // 1 => true, 0 = 0
Use:
if (0 === 'Pictures')
{
echo 'true';
}
The === is strict type operator, it not only checks the value but the type as well.
Quick Test:
if(0 == 'Pictures')
{
echo 'true';
}
else
{
echo 'false';
}
outputs true but:
if(0 === 'Pictures')
{
echo 'true';
}
else
{
echo 'false';
}
outputs false