PHP end() and Pass by Reference [duplicate] - php

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);

Related

Strange output after calling unset() [duplicate]

This question already has answers here:
unset variable in php
(5 answers)
Closed 4 years ago.
The documentation for NULL says that if I called unset() on a variable, the variable will become NULL:
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
However, this tutorial says the following will happen when calling unset() on a variable:
PHP looks in the symbol table to find the zval corresponding to this
variable, decrements the refcount, and removes the variable from the
symbol table. Because the refcount is now zero, the garbage collector
knows that there is no way of accessing this zval, and can free the
memory it occupies.
Now I tried the following code:
<?php
$x = 12345;
unset($x);
echo gettype($x);
?>
The output I got is strange, I got an error that says that the variable is undefined (which conforms with the second quote I have posted), but I also got the type of the variable which is NULL (which conforms with the first quote I have posted):
Why am I getting this strange output?
unset() destroys the specified variables.
It does not make the Variable NULL so the warning absolutely makes sense
Notice: Undefined variable: x
Reference : http://php.net/manual/en/function.unset.php
unset() destroys the specified variables. Note that in PHP 3, unset() will always return TRUE (actually, the integer value 1). In PHP 4, however, unset() is no longer a true function: it is now a statement. As such no value is returned, and attempting to take the value of unset() results in a parse error

What is the difference in the code [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
The below code is for sanitizing the posted values. Can some tell me What is the difference between,
<?php
function sanitize_data(&$value, $key) {
$value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>
and
<?php
function sanitize_data($value, $key) {
$value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>
Thanks
The first uses value as a refrence, so any time you call it with some variable the variable will be changed in the outer scope, not only in the function itself.
Look in the manual for 'reference' if you want more info.
It's called 'pass by reference'. &$value will relate to the original $value passed into the function by pointer, rather than working on a function version.
Please see the PHP Manual.
The first function the value of the first parameter is passed by reference and in the second not. If the variable is passed by referenced, changes to it will also be done on the value outside of the function scope (in the scope you call the function).
Also read the PHP documentation (pass by reference) and is also demonstrated on the array_walk doc page.
First method is called as "Passing value as reference".
So $_POST array values are changed .
In second method will not change the value of $_POST
You can check SO Link: Great Explanation about it.
https://stackoverflow.com/a/2157816/270037
The first function gets $value passed by reference so it can modify it directly, the second function gets passed $value's value.

Convert Query String to Array [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 9 years ago.
How can I convert this to an array in PHP?
&height=0&weight=2&width=10
I'm passing a data from a jquery function using .serialize() to a PHP function.
Any ideas?
Can be done within one line. :)
parse_str('&height=0&weight=2&width=10', $array);
print_r($array);
Depending on what type of request you are performing, it may already be in an array. Have a look at the PHP documentation on $_GET and $_POST global variables.
To view the contents of said array. You can use the function print_r() which will show you the contents of the array.
print_r($_GET)
print_r($_POST)
Access individual items in the array by the item's key. For example:
echo $_POST['height'];

checking if a value is a multidimensional array [duplicate]

This question already has answers here:
Checking if array is multidimensional or not?
(16 answers)
Closed 9 years ago.
How come when I run this code, I get an output of I am a multidimensional array! (the first block). I thought it would go into the second block, but it doesn't. What am I missing here?
$values = array('1','2');
if(isset($values[0][0])){
echo "I am a multidimensional array!";
}else{
echo "I am not a multidimensional array.";
}
$values = array(1,array(1,2));
$multi = false;
if(is_array($values)){
foreach($values as $k=>$v){
if(is_array($v)){
$multi = true;
break;
}
}
}
echo $multi ? "multi" : "not multi";
Try this:
if(is_array($values[0]))
Edit: This will check the first element of the array only. You should loop through each element to check if its truly multidimensional.
This code checks to see if the first element of the array is also an array. isset just checks whether or not a variable is NULL.
isset in your example is not working as expected. Perhaps there is a slight difference in functionality between PHP versions or setups. I didn't see anything in the manual but maybe you can:
http://php.net/manual/en/function.isset.php
Using is_array is more semantic, so in my opinion is a much better choice.
This code only goes into the if-branch for me, if the first value in the array is explicitly declared as a string,
$values = array('1',2);
– and with that the behavior is nothing but logical, because $values[0] is that text literal '1', and that has a first character that can be access using a zero based index.
So I guess either your real data is of a string type – or it maybe depends in the PHP version (I tested under 5.3.16).
Anyway, using is_array as the other answers already suggested is the right way to go here.

Converting objects to arrays in PHP [duplicate]

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

Categories