Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 22nd, 2005, 12:55 AM
Marcin Dobrucki
Guest
 
Posts: n/a
Default 'Parse error' problem in "isset" and "empty"

I've been having some problems with a parse error that I can't figure
out (PHP 4.3.11 on Solaris9). Sample code:

<?php
// getting strange parse errors on this
class A {
var $value;
function A() {
$this->value = 1;
}

function getValue() {
return $this->value;
}
}

$a = new A();
if (!empty($a->getValue())) {
echo "success";
}
else {
echo "failure";
}
?>

The result of this is:

Parse error: parse error, unexpected '(', expecting ')' in
/foo/public_html/parse_errors.php on line 15

I also get the same effect with "isset", however, testing with "is_null"
or "is_int" produces the correct ("success"/"failure") result. Any ideas?

/Marcin
  #2  
Old August 22nd, 2005, 03:45 AM
ZeldorBlat
Guest
 
Posts: n/a
Default Re: 'Parse error' problem in "isset" and "empty"

Check out the following:

http://www.php.net/manual/en/function.empty.php

empty() and isset() only work on variables. In your case,
$a->getValue() is the return value of a function -- not a variable.
You could do something like:

$a = new A();
$v = $a->getValue();
if (!empty($v)) {
echo "success";
}
else {
echo "failure";
}

Alternatively, you could define your own isEmpty() function which does
whatever you want. It could be as simple as:

function isEmpty($value) {
return (strlen($value) == 0);
}

Also, as of PHP 5.1 you can overload calls to empty() and isset() on
object properties. Most useful when you also overload the getters and
settings (via __get() and __set()).

  #3  
Old August 22nd, 2005, 10:55 AM
Marcin Dobrucki
Guest
 
Posts: n/a
Default Re: 'Parse error' problem in "isset" and "empty"

ZeldorBlat wrote:[color=blue]
> Check out the following:
>
> http://www.php.net/manual/en/function.empty.php
>
> empty() and isset() only work on variables. In your case,
> $a->getValue() is the return value of a function -- not a variable.
> You could do something like:[/color]

Yea, thanks. I noticed this just as I posted the article, and then
promply canceled it, but it seems it didn't get canceled everywhere.
the is_int and is_null work differently, and it threw me off a bit.
[color=blue]
> $a = new A();
> $v = $a->getValue();
> if (!empty($v)) {
> echo "success";
> }
> else {
> echo "failure";
> }[/color]

Yes, that's how I do it now. A bit of fuss, but works.
[color=blue]
> Alternatively, you could define your own isEmpty() function which does
> whatever you want. It could be as simple as:
>
> function isEmpty($value) {
> return (strlen($value) == 0);
> }[/color]

Yes, but again, "empty" checks for null, 0, etc. Which is useful.
[color=blue]
> Also, as of PHP 5.1 you can overload calls to empty() and isset() on
> object properties. Most useful when you also overload the getters and
> settings (via __get() and __set()).[/color]

Will have a lookie, but still working in 4.x level (and it's not
going to change anytime soon). Thanks.

/Marcin
  #4  
Old August 22nd, 2005, 03:15 PM
ZeldorBlat
Guest
 
Posts: n/a
Default Re: 'Parse error' problem in "isset" and "empty"

Well, that's the beauty of writing your own isEmpty()...you can have it
do whatever you want. You could simulate PHP's empty() behavior pretty
easily:

function isEmpty($value) {
return (strlen($value) == 0 || is_null($value) || $value == 0);
}

  #5  
Old August 22nd, 2005, 04:55 PM
Hero Wanders
Guest
 
Posts: n/a
Default Re: 'Parse error' problem in "isset" and "empty"

Hello!
[color=blue]
> Well, that's the beauty of writing your own isEmpty()...you can have it
> do whatever you want. You could simulate PHP's empty() behavior pretty
> easily:
>
> function isEmpty($value) {
> return (strlen($value) == 0 || is_null($value) || $value == 0);
> }[/color]

If you want to emulate PHP empty()'s behaviour completely (and only
then) you should use

function isEmpty($value) {
return empty($value);
}

I assume this is faster than doing the checks yourself.

Greetings,
Hero
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles