Php array offset passes 'isset', even though it's not set - php

The easiest way for me to explain this is to show an example ... Here's a replication of the problem code:
<?php
$test=array();
$test['one']='hello';
if(isset($test['one']['two'][0])) {
echo 'Apparently it is set ...';
echo $test['one']['two'][0];
}
?>
This returns as:
Apparently it is set ...
Warning: Illegal string offset 'two' in C:\test.php on line 6
h
Is this because there are mixed key types? It's just a little anomaly I came across and was wondering if someone could shed some light on it ...

The reason is that, when you dereference a string, it will return a string comprising a single character (assuming the index doesn't exceed the length); the resulting string can be dereferenced again (starting from 5.4 onwards).
For example - link:
$s = 'hello';
$s[0]; // "h"
$s[0][0]; // "h"
// etc. etc.
Illegal indices such as 'two' will cause a notice but it's treated as index 0, except when used inside isset().
Another example:
$s[0][1]; // ""
$s[0][1][0]; // notice: uninitialised string offset: 0
If you don't know beforehand whether a string or array is passed and this is important to you, additional type checks need to take place in between each path.

You should check your all your array keys exist before you try and use them, i.e. all the way up the chain. isset() accepts multiple parameters so you don't need to keep rewriting it and can keep DRY principles going a little more:
$test = array();
$test['one'] = 'hello';
if (isset($test['one'], $test['one']['two'], $test['one']['two'][0])) {
echo 'Apparently it is set ...';
echo $test['one']['two'][0];
}

isset returns odd and unexpected results when you pass it a string instead of an array.
It is good practice to pair an an is_array check with an isset check.

Related

PHP: Why should only variables be passed by reference?

If you are a PHP developer you most probably have seen the following notice:
Notice: Only variables should be passed by reference in /somefile.php
on line xxx
(Problem extensivley treated in Only variables should be passed by reference)
Example throwing notice:
$string = "hi-dude";
echo end(explode('-', $string));
Working example:
$string = "hi-dude";
$strings = explode('-', $string);
echo end($strings);
Explanation:
Only real variables may be passed by reference, not functions which are returning the correct variable.
However I can not think of a good reason why this notice is happening. It feels unecessary and requires me to write a lot of extra lines of code sometimes. What is the reason for PHP having this strange restriction? Why does this problem even exist?
end() or array_pop() will return the E_NOTICE with message
Only variables should be passed by reference
The reason is that end() requires a reference, because it makes the current element pointer point to the last element.
You can do it with one line,
$string = "this-is-a-sample-text";
echo substr(strrchr($string, '-'), 1);
DEMO: https://3v4l.org/jO29n
Finally I found a great explanation which helped me to understand this: What's the difference between passing by reference vs. passing by value?
As Daniel Pryden states:
In simplest terms:
call by value means that you pass values as function arguments
call by reference means that you pass variables as function arguments
In metaphoric terms:
Call by value is where I write down something on a piece of paper and hand it to you. Maybe it's a URL, maybe it's a complete copy of
War and Peace. No matter what it is, it's on a piece of paper which
I've given to you, and so now it is effectively your piece of paper.
You are now free to scribble on that piece of paper, or use that piece
of paper to find something somewhere else and fiddle with it,
whatever.
Call by reference is when I give you my notebook which has something written down in it. You may scribble in my notebook (maybe I
want you to, maybe I don't), and afterwards I keep my notebook, with
whatever scribbles you've put there. Also, if what either you or I
wrote there is information about how to find something somewhere else,
either you or I can go there and fiddle with that information.
In this case the notice "Only variables should be passed by reference" is still unjustified as we are only interested in retrieving the last value of the array. However the function end() is defined like
mixed end ( array &$array )
The & sign which states passing by reference is there for a certain reason: end() is not just returning the last element of an array, it also changes its internal pointer to the end. Therefore the array is modified.
If we only would return the last element of an array without touching the array there would be no need to pass the array by reference and we would not get this notice. But end() is somehow the wrong function for that.
What if there is no justification for me getting this notice?
Note that also the function to be called might be defined wrong. In my case I hade a function defined like this:
/**
* Flatten an array by one level if only needing a certain key value from a sub array.
*
* Example: [["foo"=>"bar","foo"=>"cheese"]]
* Result: ["bar","cheese"]
*
* #param $array: The input array.
* #param $key: The key to flatupshift. Default is 0.
* #return $array: The result
*/
private function array_flatupshift(&$array, $key = 0) {
$a = [];
foreach ($array as $item) {
if (is_object($item)) {
array_push($a, $item->$key);
} else if (is_array($item)) {
array_push($a, $item[$key]);
}
}
return $a;
}
This is simply a wrong function definition. So if you also get notices like this: Check if the function you call is defined correctly. Passing by reference does not make sense here as the array being passed is not touched in any way. Therefore the function definition should be without the "reference &/":
private function array_flatupshift($array, $key = 0) {
There are some cases where you MIGHT use the error control operator if you know what you are doing. Therefore:
$string = "hi-dude";
echo #end(explode('-', $string));
... would be o.k. I guess is the result of explode is not needed anymore. However notice the drawbacks of suppressing all possible errors. Please correct me if I go wrong here.

Why does treating integers as arrays ($int[$index]) not raise any errors in PHP? [duplicate]

This question already has an answer here:
How does the "Array dereferencing" work on a scalar value of type boolean/integer/float/string as of PHP version 7.2.0?
(1 answer)
Closed 5 years ago.
This is just a simple question out of curiosity. I spent the whole day debugging my PHP code, and found the problem was due to treating an integer as an array:
$x = $int[$index]; // this returns null, but no error at all
The integer was actually meant to be an array, but the function that was meant to pass the array messed up and passed the first value in the array instead. Is there any reason why an error isn't shown? Accessing an undefined index on an array creates an error, however trying to access a non-existent index on an integer doesn't?
I originally thought; it typecasts $int to a string because [] is another way of accessing string positions. Seems plausible that that wouldn't generate an error:
$string 'Hello';
echo $string[0]; // H
However, that's not the case:
$int = 1;
echo $int[0];
Outputs nothing and var_dump($int[0]) returns NULL.
Interestingly, the same behavior is exhibited for bool and float and the operation used is FETCH_DIM_R which is Fetch the value of the element at "index" in "array-value" to store it in "result".
From the manual Arrays:
Note: Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.
Similar to this phenomenon, where no error is generated. Bugs #68110 and #54556:
$array['a'] = null;
echo $array['a']['non-existent'];
Not surprising that assigning doesn't work:
$int = 1;
$int[0] = 2;
Warning: Cannot use a scalar value as an array
So PHP is actually attempting to access the int, bool, float as an array but not generating an error. This is from at least version 4.3.0 to 7.2.2.
Contrary to my original theory that you're invoking undefined behavior, this behavior actually is defined in the Array documentation.
Note:
Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.
In that case, it seems like there's no type juggling happening at all, so these references to documentation regarding conversion to array aren't useful in understanding this.
Explicit conversion to array is defined.
For any of the types integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue).
Automatic conversion to array is undefined according to the type juggling documenation.
Note:
The behaviour of an automatic conversion to array is currently undefined.
At first I thought this was what was happening in this case, but since salathe pointed out that this behavior is documented elsewhere, I'm not sure what "automatic conversion to array" is.
As to why this gives you a null value without throwing an error, warning, or notice, that's just how the PHP interpreter was implemented, and as far as why that is the case, it's not really answerable here. You'd have to ask the developers, and they might be able to tell you.
this is "type juggling", a "feature" of PHP.
you can read more in the official doc
EDIT
According to documentation, "The behaviour of an automatic conversion to array is currently undefined."
I run a quick test, just for curiosity:
<?php
$int = 1;
$index = 0;
$x = $int[$index];
echo gettype($int[$index]) . PHP_EOL;
var_dump($x);
and the output shows that gettype() returns NULL, so I guess that, in this case, is converted to NULL

PHP integer-like strings

when i am using string to access array index, editor show warning.
As of PHP 5.4 string offsets have to either be integers or
integer-like strings, otherwise a warning will be thrown.
can any body please explain the "integer-like strings" ? or reason for showing warning.
Additionally if same is use for $_POST it didn't show any warning.
Below snippet gives warning.
$POST = $_POST;
$POST['username']
While below didn't
$_POST['username']
I would call integer-like strings everything php can convert to an integer, basically everything.
Which editor are you using? This warning is nonsense, php has the ability to use integers or strings as array index, thus you basically can dump everthing into the index of the array (Probably not an object).
$ar = array();
$ar['a string'] = 'Content';
$ar['11string'] = 'More Content';
$ar['1'] = 'More content';
$ar[1] = 'Even more';
All of these things are allowed.
EDIT:
As ShiraNai7 pointed out, the error is about accessing strings via the array modifier, in this case you are only allowed to use integers (or as said integer-like strings). This is primary the case, because you are accessing the n-th character of an string.
Still, there is no reason to show this warning at this point. Maybe you have some more complex code at this point, override $POST to a string?

php get array elememnt using $$

i know this
$var1 = "10";
$var2 = "var1";
then
echo $$var2 gives us 10
i want to this with array
i have array
$intake_arr = array(5=>10,7=>20,8=>30,9=>40,10=>50,11=>60,12=>70);
i have some logic that will pick one array from set of array , all array will look like $intake_arr
if i do this $target_arr = "intake_arr";
then can $$target_arr[5] will yield 10? i tried but i didnt that 10 value, how can i achieve this with array
Your statement ($$target_arr[5]) is ambiguous. PHP doesn't know what you actually want to say: Do you mean: use $target_arr[5]'s value and prepend the $, to use that as a variable, or do you want to use the value of $target_arr, and get the fifth element of that array?
Obviously it's the latter, but PHP doesn't know that. In order to disambiguate your statement, you have to use curly braces:
${$target_arr}[5];
That'll yield 10. See the manual on variable variables for details
Note:
As people said in comments, and deleted answers: variable variables, like the one you're using is risky business. 9/10 it can, and indeed should be avoided. It makes your code harder to read, more error prone and, in combination with the those two major disadvantages, this is the killer: it makes your code incredibly hard to debug.
If this is just a technical exercise, consider this note a piece of friendly advice. If you've gotten this from some sort of tutorial/blog or other type of online resource: never visit that site again.
If you're actually working on a piece of code, and you've decided to tackle a specific problem using variable vars, then perhaps post your code on code-review, and let me know, I'll have a look and try to offer some constructive criticism to help you on your way, towards a better solution.
Since what you're actually trying to do is copying an array into another variable, then that's quite easy. PHP offers a variety of ways to do that:
Copy by assignment:
PHP copies arrays on assignment, by default, so that means that:
$someArray = range(1,10);//[1,2,3,4,5,6,7,8,9,10]
$foo = $someArray;
Assigns a copy of $someArray to the variable $foo:
echo $foo[0], ' === ', $someArray[0];//echoes 1 === 1
$foo[0] += 123;
echo $foo[0], ' != ', $someArray[0];//echoes 123 != 1
I can change the value of one of the array's elements without that affecting the original array, because it was copied.
There is a risk to this, as you start working with JSON encoded data, chances are that you'll end up with something like:
$obj = json_decode($string);
echo get_class($obj));//echoes stdClass, you have an object
Objects are, by default, passed and assigned by reference, which means that:
$obj = new stdClass;
$obj->some_property = 'foobar';
$foo = $obj;
$foo->some_property .= '2';
echo $obj->some_property;//echoes foobar2!
Change a property through $foo, and the $obj object will change, too. Simply because they both reference exactly the same object.
Slice the array:
A more common way for front-end developers (mainly, I think, stemming from a JS habbit) is to use array_slice, which guarantees to return a copy of the array. with the added perk that you can specify how many of the elements you'll be needing in your copy:
$someArray = range(1,100);//"large" array
$foo = array_slice($someArray, 0);//copy from index 0 to the end
$bar = array_slice($someArray, -10);//copy last 10 elements
$chunk = array_slice($someArray, 20, 4);//start at index 20, copy 4 elements
If you don't want to copy the array, but rather extract a section out of the original you can splice the array (as in split + slice):
$extract = array_splice($someArray, 0, 10);
echo count($someArray);//echoes 90
This removes the first 10 elements from the original array, and assigns them to $extract
Spend some time browsing the countless (well, about a hundred) array functions PHP offers.
${$target_arr}[5]
PHP: Variable variables
Try this one:
$intake_arr = array(5=>10,7=>20,8=>30,9=>40,10=>50,11=>60,12=>70);
$target_arr = 'intake_arr';
print ${$target_arr}[5]; //it gives 10
For a simple variable, braces are optional.But when you will use a array element, you must use braces; e.g.: ${$target_arr}[5];.As a standard, braces are used if variable interpolation is used, instead of concatenation.Generally variable interpolation is slow, but concatenation may also be slower if you have too many variables to concatenate.Take a look here for php variable variables http://php.net/manual/en/language.variables.variable.php

php, how to treat arrays with floats inside?

example:
the following is correct?
$var = floatval($arr[2]) ;
"You cannot use floatval() on arrays..." maybe that's an old directive or how to edit...?
That quote from the manual (which BTW doesn't seem to exist in the current manual anymore) only means that you can't use floatval on values that are arrays, i.e.:
$foo = array();
$bar = floatval($foo);
Which, BTW, is not entirely correct, since it would produce either 1.0 or 0.0, depending on whether the array was empty or not.* It just doesn't make much sense. If you access a scalar value inside an array, that's not using "floatval on an array". I.e., this works perfectly fine:
$foo = array("42.1231");
$bar = floatval($foo[0]);
That's using the scalar value in $foo[0], whether that's in an array or not is irrelevant.
* The manual now clearly says Empty arrays return 0, non-empty arrays return 1. Maybe this behavior has changed?
you will need to traverse the array and perform the operation on each element:
foreach($arr as $id=>$elem){
$arr[$id]=floatval($elem);
}

Categories