This question already has answers here:
Increment behavior on strings - PHP easter egg?
(3 answers)
Closed 7 years ago.
$a = 'z99';
var_dump(++$a);
Above code will output aa00.I'm very confused about this output. Can somebody help me?
That's because PHP follows Perl's convention.
You can find more information on http://php.net/manual/en/language.operators.increment.php
So because this is a string the 99 goes to 00 -> next increment and the Z goes to AA. If you would have done it with Y99 it would have been Z00.
$s = 'W';
for ($n=0; $n<6; $n++) {
echo ++$s . PHP_EOL;
}
Returns:
X
Y
Z
AA
AB
AC
Note that decrementing strings won't work in PHP.
$s = 'W';
for ($n=0; $n<6; $n++) {
echo --$s . PHP_EOL;
}
Returns:
W W W W W W
Related
This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);
This question already has answers here:
Is there a PHP function for swapping the values of two variables?
(20 answers)
Closed 9 years ago.
I want to share 1 question which is asked most of the time in interviews and I wasn't able to e answer that question, but finally I found the answer:
How to Swap 2 variable value without using 3rd variable??
This method will work for any variable type:
$a = 5;
$b = 6;
list($a, $b) = array($b, $a);
print $a . ',' . $b;
Output:
6,5
Another simple way (which only works for numbers, not strings/arrays/etc) is
$a = $a + $b; // 5 + 6 = 11
$b = $a - $b; // 11 - 6 = 5
$a = $a - $b; // 11 - 5 = 6
print $a . ',' . $b;
Output:
6,5
Surely you want the XOR swap algorithm ? At least for numeric variables.
Conventional swapping requires the use of a temporary storage
variable. Using the XOR swap algorithm, however, no temporary storage
is needed. The algorithm is as follows:
X := X XOR Y
Y := X XOR Y
X := X XOR Y
Although I've never seen it used in real scenarios (beyond assembler work)
This question already has answers here:
what is 0050 and why echo 0050 result 40 [duplicate]
(2 answers)
Closed 9 years ago.
While coding I got an unexpected result.
I'm unsure how to ask this question so I'll put in my code and result:
$variable = 012;
$variable2 = 12;
$variable3 = '012';
When I echo out the variables:
Expected result:
$variable: 12
$variable2: 12
$variable3: 012
Result:
$variable: 10
$variable2: 12
$variable3: 012
What is happening here?
See the documentation of the integers of php
Then you see the following:
To use octal notation, precede the number with a 0
So octal 12 is decimal 10
Little bit more info of the docs
<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$variable is Octal
$variable2 is int
$variable3 is string
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I found the following line of code in a PHP script and have never seen anything like it before:
$a = ($ba%10)
What does this do?
Its is PHP's Arithmetic Operators
The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a. For example:
<?php
echo (5 % 3)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2
?>
Click PHP.NET for more information!
It tells you the remainder of a division calculation. So 25%8 would be 1. If $ba = 101 then $ba%10 would equal 1.
% is the modulus operator, it gives you the remainder of integer division.
e.g. 87 % 10 = 7
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
What does adding a '&' mean in this snippet I found?
$i = 10;
if($i&1){
echo "$i is odd";
}
else {
echo "$i is even";
}
It's the bitwise AND operator. in your case, it takes the binary representations of 10 and 1 and performs the logical AND operation on the individual bits.
That's a binray and. So for example 148 (binary 10010100) & 136 (binary 10001000) will be 128 (binary 10000000). So $i & 1 is either 1 (true) or 0 (false)