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

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";
}

Related

Getting Can't use function return value in write context [duplicate]

This question already has answers here:
Weird PHP error: 'Can't use function return value in write context'
(12 answers)
Closed 6 years ago.
I get the error in the subject. Also I spent ages on google and found dozens of resources having the same error, but still I can't figure out what the issue is.
This is my code:
<?php
if(empty(trim($_POST["user"])) || empty(trim($_POST["text"]))) {
echo "no luck";
}
?>
PHP Fatal error: Can't use function return value in write context in
/var/www/test.php on on line 2
If you refer to a manual, you will see
Determine whether a variable is considered to be empty.
The result of trim passed to empty is not a variable.
So your options are:
$user = trim($_POST['user']);
if (!empty($user)) { }
Or php5.5, in which
empty() now supports expressions

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 is if(empty(strlen(trim($_POST['name'])))) invalid? [duplicate]

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']) === '') ...

how to forbid the Fatal error: Call to a member function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Any reason why Mage::registry(‘current_category’) would return NULL?
Reference - What does this error mean in PHP?
Fatal error: Call to a member function getParentCategory() on a non-object in...
the code:
$_category_detail=Mage::registry('current_category');
$id=$_category_detail->getParentCategory()->getId();
now, when the page can't use getParentCategory() i using the following but can't work.
if( isset(getParentCategory()){
$id=$_category_detail->getParentCategory()->getId();
}
why? thank you
It appears that $_category_detail is not an object. Therefore Mage::registry('current_category') is not returning an object.
It's most likely returning some sort of NULL or false value upon fail. And PHP is making you notice that (NULL)->getParentCategory() is meaningless.
In your particular case it returns NULL because current_category is not set in your registry.
You need to use method_exists() rather than trying to call a non-existent function:
if (method_exists($_category_detail, "getParentCategory"))
isset() only checks for member variables. Use method_exists().
PHP Manual: http://php.net/manual/de/function.method-exists.php
if (method_exists($_category_detail, 'getParentCategory')) {
$id = $_category_detail->getParentCategory()->getId()
}

properties as array [duplicate]

This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 9 years ago.
I want to do something like this without using extra variables:
class className {
public static function func(){
return array('true','val2');
}
}
if(className::func()[0]) {
echo 'doğru';
} else {
echo 'Yanlış';
}
className::func()[0] is called array dereferencing, and is not valid syntax in all PHP versions. It will be is available starting in PHP 5.4, currently in beta, released March 2012. For earlier PHP version, you will need to use an extra variable somewhere to store the array returned from className::func().
See the PHP 5.4 Array documentation for implementation details.
Array Deferencing is not currently available in PHP. It is on the table for PHP 5.4.
Until then, you would need the extra variable:
$arr = className::func();
if($arr[0]){
echo 'doğru';
}else{
echo 'Yanlış';
}
Well,you can return an object in your method instead.
something like:
class className{
public static function func(){
return (object)array('true'=>'true','val2'=>'val2');
}
}
echo className::func()->true;//Extra variables go away =)
As the others noted, you currently cannot do it this way. If you really cannot use an temporary variable (although I don't see a reason not to use one) you could use
if(current(className::func())) // or next() or reset()
But make sure you read the documentation about these functions to treat empty arrays properly.
Reference: current

Categories