Why is if(empty(strlen(trim($_POST['name'])))) invalid? [duplicate] - php

This question already has answers here:
Can't use method return value in write context
(8 answers)
Closed 8 years ago.
I have a if statement check to see if a string is empty
if(empty(strlen(trim($_POST['name'])))){
$error_empty = true;
}
gives me this error:
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\requestaccess\index.php on line 51

empty is not a function -- it's a "language construct" that prior to PHP 5.5 can only be used to evaluate variables, not the results of arbitrary expressions.
If you wanted to use empty in exactly this manner (which is meaningless) you would have to store the result in an intermediate variable:
$var = strlen(trim($_POST['name']));
if(empty($var)) ...
But you don't need to do anything like this: strlen will always return an integer, so empty will effectively be reduced to checking for zero. You don't need empty for that; zero converts to boolean false automatically, so a saner option is
if(!strlen(trim($_POST['name']))) ...
or the equivalent
if(trim($_POST['name']) === '') ...

Related

Checking if variable exist and if it has a certain value in one line [duplicate]

This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 months ago.
I have always thought that if I want to check if a variable exists and has a certain value I have to use two if conditions:
if(isset($x)){
if($x->age==5){}
}
But I realized its also possible to do it in one line this way:
if(isset($x) && ($x->age==5)){}
Can someone tell me why the second variation will not result in an error if $x is null. Given that $x is null and doesn't have the property age? Would it be trying to access a property that doesn't exist?
$x=null;
Because $x is null, isset($x) is false. Then, because of the logical operator "AND" (&&), the condition cannot be fully validated, so, the test is stopped here and ($x->age==5) is not executed.
For a shorter code, as of PHP 8.0.1, you can use the NullSafe Operator (?->)
if ($x?->age == 5) { }

How to safely chain methods? [duplicate]

This question already has answers here:
Is there a "nullsafe operator" in PHP?
(3 answers)
Closed 2 years ago.
Is there a way to "safely" chain methods in PHP and simply return null if some previous method returns null? Otherwise, an error would be thrown: trying to get property on non-object;
For example, the following code checks whether a customer's phone number has changed using a QuickBooks SDK. I don't have control over these methods.
$customer->getPrimaryPhone() will always return an object since the form wouldn't have been submitted otherwise, but $old->getPrimaryPhone() may return null if no phone number existed previously.
The following is required to get the phone number:
$old->getPrimaryPhone()->getFreeFormNumber()
If getPrimaryPhone() returns null, then an error would be thrown.
My question is: How would I avoid code repition in the following case?
if (!empty($old->getPrimaryPhone())) {
if ($customer->getPrimaryPhone()->getFreeFormNumber() !== $old->getPrimaryPhone()->getFreeFormNumber()) {
// Repetive code here
}
} else {
// Repetive code here
}
I'd be inclined to implement an equals method on your PhoneNumber class (or whatever it's called). For example
public function equals(PhoneNumber $otherNumber) {
return $otherNumber !== null && $this->getFreeFormNumber() === $otherNumber->getFreeFormNumber();
}
Then you can simply use
if (!$customer->getPrimaryPhone()->equals($old->getPrimaryPhone())
If you've got other logic that needs to be applied (as indicated in your comment about arrays), you can easily implement that in the equals method.

Why this expression return error and how can I resolve? [duplicate]

This question already has answers here:
Weird PHP error: 'Can't use function return value in write context'
(12 answers)
Closed 9 years ago.
This code:
if(!empty(trim($_POST['post']))){ }
return this error:
Fatal error: Can't use function return value in write context in ...
How can I resolve and avoid to do 2 checks ( trim and then empty ) ?
I want to check if POST is not only a blank space.
you cant use functions inside isset , empty statements. just assign the result of trim to a variable.
$r = trim($_POST['blop']);
if(!empty($r))....
edit: Prior to PHP 5.5
if (trim($_POST['post'])) {
Is functionally equivalent to what you appear to be trying to do. There's no need to call !empty
if (trim($_POST['post']) !== "") {
// this is the same
}
In the documentation it actually explains this problem specifically, then gives you an alternate solution. You can use
trim($name) == false.
In PHP, functions isset() and empty() are ment to test variables.
That means this
if(empty("someText")) { ... }
or this
if(isset(somefunction(args))) { ... }
doesn't make any sence, since result of a function is always defined, e.t.c.
These functions serve to tell wether a variable is defined or not, so argument must me a variable, or array with index, then it checks the index (works on objects too), like
if(!empty($_POST["mydata"])) {
//do something
} else {
echo "Wrong input";
}

Return a value if a PHP GET doesn't exist? [duplicate]

This question already has answers here:
Check if url contains parameters [duplicate]
(3 answers)
How to verify if $_GET exists?
(7 answers)
Closed 9 years ago.
I have lots of PHP statements along the lines of:
$getvalue = $_GET['valueiwant'];
In some scenarios not all variables are available. So, let's say 'valueiwant' doesn't exist in the URL string, how can I return a value based on the fact it doesn't exist?
For example if 'valueiwant' can't be found set $getvalue to -1
Currently it appears the value defaults to 0 and I need to be equal less than 0 if it doesn't exist.
Any ideas?
thanks
I always use
$getvalue=isset($_GET['valueiwant'])?$_GET['valueiwant']:-1;
Use of the isset() function checks if the offset exists, and returns a boolean value indicating it's existence.
This means that you can structure an if statement around the output.

IF Statement always executed in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does PHP consider 0 to be equal to a string?
php string comparasion to 0 integer returns true?
it seems that as one has in PHP an if-statement where a function some_function() returns zero
<?php
if( some_function() == "whatever_you_want" ) { ... }
the statement will always be executed since
<?php
echo some_function() == "whatever_you_want";
is then TRUE.
Why behaves PHP in such a counter intuitive way?
This is a defined behavior of PHP when you compare a number value and a string value:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
Use strict value comparison with === or !== and you’re getting the expected result.

Categories