Why doesn't end(( )) throw a strict notice? [duplicate] - php

This question already has answers here:
Parentheses altering semantics of function call result
(2 answers)
Closed 8 years ago.
end(array_keys(array(0))) says PHP Strict standards: Only variables should be passed by reference ( http://3v4l.org/CNLVT )
end((array_keys(array(0)))) on the other hand, just works ( http://3v4l.org/168fi ). Why?
The VLD decompiler shows the same opcodes being ran the only difference is in the ext column but I can't find documentation on what that means.

What's likely happening is array_keys is passing the result back by reference. As such, PHP is throwing you a notice that you shouldn't do that.
Wrapping in parenthesis actually changes the reference and forces PHP to evaluate the statement inside first. As such, it removes the reference. One of those weird things that doesn't look like it makes a difference but actually does.
More on the weirdness here http://phpsadness.com/sad/51

Related

PHP count replacement [duplicate]

This question already has answers here:
PHP count JSON array
(4 answers)
Closed 4 years ago.
I have some code that works fine on servers running below PHP 7, but on PHP 7 I get a warning that I need to get rid of. I need to fix the code to get rid of the warning, I can not just hide the warnings.
My issue is with the count() function. Here is the warning I am getting and the little bit of code it is referring to. The array has the possibility of having many elements, some with values and others with blank values. It is also possible that the array will be empty. I assume that when the array is empty, that is when the warning is triggered. So I am looking for a way to tell if the array has 1 or more elements, with and without blank values. As long as there is one key then the if statement should be true.
PHP Warning: count(): Parameter must be an array or an object that implements Countable
$tb_operator_meta_json = get_post_meta($tableid, 'tb_operator_meta', true);
$tb_operator_meta = json_decode($tb_operator_meta_json, true);
$tb_operator_meta = wp_unslash($tb_operator_meta);
if (count($tb_operator_meta) > 0 && $tb_operator_meta != null) {
I don't know why this was marked as a duplicate. If you read my post it is clearly not the same as the other post.
As of PHP 7.2.0
count() will now yield a warning on invalid countable
types passed to the array_or_countable parameter.
http://php.net/manual/en/function.count.php
check the array is_array() before counting.
check if it is an array or not null.
use is_array($var);
or
use (!empty($var))

'&' sign before function name in PHP [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.

PHP: why are some internal functions missing from `get_defined_functions`? [duplicate]

This question already has answers here:
What is the difference between a language construct and a "built-in" function in PHP?
(4 answers)
Closed 8 years ago.
PHP has a large number of batteries-included functions, e.g. functions on arrays. Some of these, like each, are present in get_defined_functions()['internal']. Others, like reset and many others, are not present at all. However, they are treated as functions in every other way: they are not documented as "language constructs" or keywords; I can call them using the "variable function" feature; function_exists("reset") returns true; if I try to redefine them (e.g. function reset() { ... }), I get an error about redeclaration, rather than a syntax error; and so on.
Why are these functions not listed by get_defined_functions? Are they not actually functions? If not, what are they? If they are functions, then what actually is it that get_defined_functions is listing? In either case, how do I list the things that don't appear in get_defined_functions?
Quite a short answer: Reset is present in get_defined_functions()['internal'].
Look at [1532] in this fiddle: http://phpfiddle.org/main/code/h5n-ndx

When to use #, and different ways to declare variables? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I'm learning object oriented php.
I've come across some code I don't fully understand.
This code is within a class.
1) code that uses #.
For example:
$this->image = #imagecreatefromgif($filename);
Can some one explain the uses of #.
2)
Also it looks like the script is declaring variables in a way i'm not used to (the $var way).
For example:
$this->ext = $size['mime'];
$ext is not declared before this code is used, but it used after it. Does this create an $ext variable within the object?
Thanks guys!!
The # will suppress errors so that no errors will be shown for that expression.
http://php.net/manual/en/language.operators.errorcontrol.php
# is php's error suppression operator you should never use it
You should handle error instead ignoring and advantage would be you will get long notice which is helpful to debug too
And worst case would be below as described in manual
Currently the "#" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "#" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
# Means "Suppress warning when calling this function".

cakephp PHP 5.4.4 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Declaration of Methods should be Compatible with Parent Methods in PHP
I just installed php 5.4.4. and I all of a sudden get a strict warning.
Does someone know what it is?
Strict (2048): Declaration of User::beforeSave() should be compatible with Model::beforeSave($options = Array) [APP/Model/User.php, line 3]
In APP/Model/User.php, change the declaration to match the class it extends, Model
function beforeSave( array $options ){
...
If you look closely, you'll notice that the methods signature differs. Model::beforeSave() accepts an optional array, whilst your method doesn't accept anything.
However, the message is not severe. It's a strict, meaning that you're breaking standards, but is not like you're on Titanic.

Categories