PHP syntax for dereferencing function result
This is specifically array dereferencing, which is currently unsupported in php5.3 but should be possible in the next release, 5.4. Object dereferencing is on the other hand possible in current php releases. I'm also looking forward to this functionality!
Why can't I reference keys in the result of this PHP function?
This is only available in PHP 5.4. It is known as array dereferencing. Prior to 5.4 you have to store the return value and then access the array elements.
Docs
PHP 5.4.0 offers a wide range of new features:
- Function array dereferencing has been added, e.g. foo()[0].
If you try this on any version below 5.4, you will get this error:
Parse error: syntax error, unexpected '[' on line ...
Function call dereferencing
This is covered in this RFC by nikic. Hope they'll get it implemented sometime soon.
How does the Array dereferencing work on a scalar value of type boolean/integer/float/string as of PHP version 7.2.0?
They are referring to non-complex types such as int or float.
In your example you are using an array. So you don't see the issue.
<?php
function getArray() {
return array(1, 2222, 3);
}
$secondElement = getArray()[1]; // 2222
$null = $secondElement[123456]; // 123456 can be any number or string
var_dump($null);
// similarly:
$also_null = getArray()[1][45678];
var_dump($also_null);
The first pair of brackets is array-dereferencing on the array (1, 2222, 3), the second is array-dereferencing on an integer (2222) which always returns null.
Simplified:
<?php
$a = 123456;
var_dump($a[42]); // is null
$a = 123.45;
var_dump($a[42]); // is null
$a = true;
var_dump($a[42]); // is null
$a = null;
var_dump($a[42]); // is null
This "fails silently" as in theory you should get an error from this, rather than just null.
Also happens with null, other than int, float, bool:
<?php
$a = true;
var_dump($a[42][42][42][42][42][42][42][42]); // also null, and no errors
But works correctly instead with arrays and strings.
<?php
$a = "abc";
var_dump($a[1]); // b
$a = [11, 22, 33];
var_dump($a[1]); // 22
Answering your question, "How does the “Array dereferencing” work on a scalar value of type": It doesn't, it just returns null instead of returning an error of some sort.
In PHP are recalls to any given function the variable they return or a call to the function itself
In PHP version >= 5.4.0 it will not result in error, but will echo p_hello
. You can check it here, changing the version number:
http://sandbox.onlinephpfunctions.com/code/db319c296ea4eb9500b9fa16bccd8f927c733d14
It is called Function Array Dereferencing:
https://wiki.php.net/rfc/functionarraydereferencing
PHP use function return value as array
PHP does not support the function array dereferencing, which you want to do.
See an RFC on the subject http://wiki.php.net/rfc/functionarraydereferencing, and the associated mailing list conversations, which was declined roughly this time last year. While there was support for this, and it's still a common request (usually "why doesn't this work?"), there are no plans in motion to introduce this syntax.
As for why, quite simply the code implementing it has not been submitted for approval (if it has been written at all).
Update
This feature has been implemented in the trunk (main development) branch of PHP and will likely be included in the next non-bugfix version (5.4.0). For anyone wanting to play with FAD, feel free to download a trunk snapshot.
mysqli_fetch_row - using it as an array - PHP
Because mysqli_fetch_row is a function that returns a value - it's not an array itself.
Assigning a variable will retrieve the result. Unfortunately that's how it works in earlier versions of PHP.
Gerald's link identifies the functionality it as availble in PHP 5.4, so I guess that you're using an earlier version.
Related Topics
How to Use Multiple Databases in Laravel
In PHP, What Is a Closure and Why Does It Use the "Use" Identifier
Pdoexception Sqlstate[Hy000] [2002] No Such File or Directory
Parse Query String into an Array
Replace Keys in an Array Based on Another Lookup/Mapping Array
How to Upload File Using Curl With PHP
How to Find the Last Day of the Month from Date
Make All Words Lowercase and the First Letter of Each Word Uppercase
I Have 2 Dates in PHP, How to Run a Foreach Loop to Go Through All of Those Days
Error When Preparing a Multiple Insert Query
When Should I Use 'Self' Over '$This'
What Is the Use of the @ Symbol in PHP
How to Get Body of a Post in PHP
PHP MySQLi_Connect: Authentication Method Unknown to the Client [Caching_Sha2_Password]
Property [Title] Does Not Exist on This Collection Instance
Recursive Function to Generate Multidimensional Array from Database Result