This question already has an answer here:
Populating associative arrays
(1 answer)
Closed 7 years ago.
Running into an issue with a simple PHP script and I can't seem to figure it out. States its on line 3 and I don't see it. Need a fresh set of eyes please.
<?php
$numArr = [];
for ($i=0;$i<5;$i++) {
array_push($numArr,mt_rand());
}
echo min($numArr);
?>
It's likely how you're initializing your array. The [] syntax is only available from PHP 5.4 and above.
From the PHP manual:
As of PHP 5.4 you can also use the short array syntax, which replaces
array() with [].
You can use array () instead.
Related
This question already has answers here:
Colon after method declaration?
(2 answers)
Closed 4 years ago.
It's almost impossible for me to search for an answer to this as I don't know what this syntax is called.
I have a server running a relatively old version of PHP (5.5.38) and I'm getting parse errors when I run a file that has the following:
public function foo(): array
{
...specifically the : array suffix.
Can anyone tell me what this syntax is called (so I can research further) and which PHP version introduced it?
This was added in PHP 7, they're called return type declarations.
http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 8 years ago.
I was wondering that we have this function in PHP:
mysqli_fetch_row($result);
and this returns a simple array, but using it as an array like
mysqli_fetch_row($result)[0];
to get the first element of the array doesn't work, so you have to create a new variable which will contain the returned array.
Could anyone explain why isn't this working?
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.
This question already has an answer here:
Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?
(1 answer)
Closed 8 years ago.
I wrote a code that worked perfectly fine, but then I reinstalled my operating system, and now when I try to run this, I keep getting this error. I am using IIS 8.0, WebMatrix, and PHP 5.3.
This is the piece of code in question:
<?php
if (!$me->get_details()['quote']) : //<--error is here
?>
<p class="quote">Write some motivational quote.</p>
<?php
else :
?>
<p class="quote"><?= $me->get_details()['quote']?></p>
<?php endif; ?>
Function get_details() returns an associative array with data from the database.
What could possibly go wrong here?
You cannot dereference in PHP 5.3 this way.
You need to do:
$result = $me->get_details();
if (!$result['quote']) :
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Access array returned by a function in php
Trivial question; is it possible to get the current index of a returning array from a PHP function, like JavaScript can like this:
function returnSomething()
{
return ['one', 'two', 'three'];
}
var two = returnSomething()[1]; // two
I've always wondered if PHP can do this, I've tried ages ago (but it is invalid to do so), and never got to ask here.
PHP can do this starting from 5.4; it's called array dereferencing.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP syntax for dereferencing function result
If a PHP function returns an array, the following syntax will not work:
$firstValue = $object->methodThatReturnsArray()[0]; // syntax error, unexpected '['
This, however, works fine:
$temporaryArray = $object->methodThatReturnsArray();
$firstValue = $temporaryArray[0]; // temporary will never be reused
What is the best syntax to solve this problem, or is creating that variable the recommended approach?
Variable is the best approach.
Still PHP 5.4 adds the feature to be able using the first mentioned syntax.
Well, you can have list to the left of the = (list($firstValue) = $object->methodThatReturnsArray();), if you need anything too deep in the array, a temporary variable is your only option.