This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
Seen in functions where $a is a function parameter:
if(!is_array($a))
$a=[$a]
I just don't know what this means,
Thanks!
It means;
if $a is not an array, then create $a as an array and use the contents (value) of $a as the first element of the newly created array called $a.
In readable English emulating a Code, this could mean:
<?php
if($a IS NOT AN ARRAY):
THEN CREATE A NEW VARIABLE $a OF TYPE: ARRAY.
TAKE WHATEVER IS INSIDE THE INITIAL $a VARIABLE...
AND PUT IT AS THE FIRST ELEMENT OF THE OVERRIDDEN, NEW $a VARIABLE.
endif;
This code convert $a from some non-array data-type to array data-type
if(!is_array($a)) \\check whether $a is not an array
$a=[$a] \\change $a to an array with only one element which was previously stored in $a
It takes a simple variable and turns it into an array.
[$a] is shorthand for array($a).
If $a is not an array, [$a] turns it into an array.
Related
This question already has answers here:
How to get the last element of an array without deleting it?
(33 answers)
What is an array internal pointer in PHP?
(1 answer)
PHP behavior and arrays pointers
(2 answers)
Closed 11 months ago.
I attempted to pass an explode statement into end() and got the following notice from my IDE, "Only variables can be passed by reference", which is not surprising.
However, what confuses me is that if I do something like
$array = ['cat', 'dog', 'bird'];
end($array);
echo print_r($array, true);
['cat', 'dog', 'bird'] prints out.
It is/was my understanding that passing something by references changes the value of the variable so I had expected $array to only print out 'Bird' after being passed into end().
I suspect I have a fundamental misunderstanding of passing something by reference...
The end() function moves the internal pointer to, and outputs, the last element in the array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.
end() function returns a value and does not change the reference array!
so you may save the value to another variable first. in fact it returns the value of the last element or false for empty array.
so you may try:
$array = ['cat', 'dog', 'bird'];
$last = end($array);
echo print_r($last, true);
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
I am facing an unexpected issue with the increment operator in PHP. Please take a look at the given two programs:
1st Program:
<?php
$a=5;
$a++;
echo $a;
?>
it prints 6, which I clearly understood that what was happened, it just incremented the value with 1.
2nd Program:
<?php
$a=5;
$b = $a++; // just assigned incremented value to a new variable b.
echo $b;
?>
it prints 5.
Now here is the confusion, I just assigned the incremented value to the variable, so I should print 6 - why it is printing 5?
You are getting 5 because in postfix operator first it will assign the value to $b after that their value will be incremented. SO first $a is assigning to $b after that $a value will increamented
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
I'm touching up on some PHP that I sometimes forget (keep the old brain going) and came across this in a PHP questionnaire. It goes as follows:
<?php
$a = "b";
$b = "a";
print ${$b} ;
//$b = "b"
?>
How does this work and how would I use it practically? Thank you in advance.
This is a variable variable
print ${$b}
It first evaluates {$b} and gets 'a'. So then it evaluates $a and gets "b" (the value stores in $a).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Diddling with arrays with numeric string keys
As specified in PHP's manual, we can do type casting on objects and covert them to array as follows:
$arrayResult = (array)$someObject;
But I found very interesting remark in documentation:
If an object is converted to an array, the result is an array whose
elements are the object's properties. The keys are the member variable
names, with a few notable exceptions: integer properties are innaccessible
What does "integer properties" stands for?
I believe this means that you cannot use typical integer properties of arrays to iterate through, such as in a for loop. The elements are not integer-indexed.
The below is the example:
$obj = new stdClass;
$obj->{'1'} = 1;
$arr = (array) $obj;
var_dump($arr);
var_dump(isset($arr[1])); // will get false
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Hi,
I was using like
$formCheckbox = $form->addElement('radio',...);
$formCheckbox->setChecked(true);
this was working on windows properly but was not on linux i.e it was not checking the radio button.
so i changed it like
$formCheckbox = **&**$form->addElement('radio',...);
$formCheckbox->setChecked(true);
so i just used & while creating the element. I just wanted to know how does it make a difference. I am using HTML quick forms.
in this case there is not much of a difference, because php handles objects internally as pointers..
but as long as you do not know what & stands for, don't use it..
a short introduction to pointer:
$a = 10;
$b = &$a;
$b = 20;
echo $a; -> 20
$a = 10;
$b = $a;
$b = 20;
echo $a; -> 10
so with & you only reference to another variable, instead of creating a new one
It is passing the variable as a reference, so that the changes are maintained always.
Check this http://php.net/manual/en/language.references.pass.php