This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've been learning PHP by reading all sorts of things on the web. I've trying to figure out what this is called so I can look it up and read about it and learn how it works. It's the "++" in the code I'm assuming counts up so that the codes knows when to do a new line. I not looking for corrections to this code I'm looking for what the "++" is called so I can look it up.
$i = 0
if (++$i == 10) {
echo "new line";
}
It's called the prefix increment unary operator. It increments the number or character, then returns its value.
The postfix version does pretty much the same but returns its value first, then increments the variable.
That is a pre-increment. So in that code, $i is first incremented (increased by one), and then its value is used to test for equality to 10.
So the code that you show essentially tests if 1 equals 10 (which it does not.)
It is a number operator named the prefix increment. It takes the previous stated number and adds 1 to it. You can find more information about arithmatic operators here: Howtoforge.com
It's the pre-increment operator. It will add one to the variable it precedes, and return the result. The post-increment operator returns the current value, and then adds one. The PHP Manuel has an article on all of the operators.
All PHP Operators
Incrementing/Decrementing Operators
Related
This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 months ago.
I have always thought that if I want to check if a variable exists and has a certain value I have to use two if conditions:
if(isset($x)){
if($x->age==5){}
}
But I realized its also possible to do it in one line this way:
if(isset($x) && ($x->age==5)){}
Can someone tell me why the second variation will not result in an error if $x is null. Given that $x is null and doesn't have the property age? Would it be trying to access a property that doesn't exist?
$x=null;
Because $x is null, isset($x) is false. Then, because of the logical operator "AND" (&&), the condition cannot be fully validated, so, the test is stopped here and ($x->age==5) is not executed.
For a shorter code, as of PHP 8.0.1, you can use the NullSafe Operator (?->)
if ($x?->age == 5) { }
This question already has answers here:
How to increment a number inside foreach loop?
(5 answers)
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm trying to do simple addition on a variable within a string, using string parsing, like so:
$a = 2;
echo("I have {$a+1} eyes");
But PHP crashes when I do this.
I also tried {++$a}, but that outputs as I have {++2} eyes.
The PHP manual page for string parsing has no example covering arithmetic within a string.
Is this something that's possible?
UPDATE: I have to disagree with the duplicate question flag. That question is titled "How to increment a number inside foreach loop?". While the answers to that question do also answer mine, search engines will not find that question from a query like "How to increment in a PHP string", or "Arithmetic with PHP string interpolation". Even if the answers are the same, the questions are different.
It's not possible through variable interpolation. It does not accept operations as arguments. What it does accept can be read in this question.
Alternatives:
Store the result into a variable first. A bit of a redundant step, but is a way to do it with interpolation.
$a = 2;
$b = $a + 1;
echo "I have {$b} eyes";
Use concatenation. Don't forget to wrap it in parentheses, because math operators don't have precedence over concatenation.
$a = 2;
echo "I have ".($a + 1)." eyes";
Use a format printing function. Note that printf outputs the string, while sprintf returns it, so you'd have to explicitly output the latter.
$a = 2;
printf("I have %d eyes", $a + 1);
echo sprintf("I have %d eyes", $a + 1);
This question already has answers here:
Why does 1...1 evaluate to 10.1? [duplicate]
(4 answers)
Closed 3 years ago.
Just a second ago I was playing around with PHP, trying to figure out if there was a native range function (eventually finding range). However, one of the things I tried was the following:
echo 1...2;
which to my surprise returns the string "10.2". Can anyone tell me exactly what syntax is responsible for this? It doesn't seem like a valid place for a splat operator.
The statement consists of three parts: 1., . and .2. The first one evaluates to the number 1, the second one is the string concatenation operator, and the latter one evaluates to 0.2. Thus, you get 10.2.
Equivalent example code:
$a = 1.;
$b = .2;
echo "a = $a\n";
echo "b = $b\n";
echo "a.b = ".($a.$b)."\n";
outputs
a = 1
b = 0.2
a.b = 10.2
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The 3 different equals
Can anyone tell me why, when using the code below, I am getting redirected to elephant.com rather than seeing a 'giraffe!
<?php
$foo="giraffe";
if($foo="elephant"){
header("location:http://www.elephant.com");
exit();
}else{
echo $foo;}
?>
Thanks for looking
J
if($foo="elephant")
You're assigning $foo here, rather than comparing it; you should be doing:
if($foo=="elephant")
The result of an assignment operation is the value that's just been assigned; in this case, 'elephant' is evaluating to true.
Your if() statement has a single equal sign. This doesn't do a comparison in PHP; it sets the value and returns true.
In order to do a comparison, you need to use either a double-equal or a triple-equal sign:
if($foo == "elephant") { .... }
or
if($foo === "elephant") { .... }
The difference between the two is that double-equal doesn't care about the variable's data type, whereas triple-equal does. In this case, there's not much difference between them, but it's worth learning and understanding the differences because they can bite you if you don't know them. More info here: http://php.net/manual/en/language.operators.comparison.php