473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

'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
Aug 21 '05 #1
4 2652
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()).

Aug 22 '05 #2
ZeldorBlat wrote:
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:
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.
$a = new A();
$v = $a->getValue();
if (!empty($v)) {
echo "success";
}
else {
echo "failure";
}
Yes, that's how I do it now. A bit of fuss, but works.
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);
}
Yes, but again, "empty" checks for null, 0, etc. Which is useful.
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()).


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

/Marcin
Aug 22 '05 #3
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);
}

Aug 22 '05 #4
Hello!
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);
}


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
Aug 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: newbie_mw | last post by:
Hi, I need urgent help with a novice problem. I would appreciate any advice, suggestions... Thanks a lot in advance! Here it is: I created a sign-up sheet (reg.html) where people fill in their...
0
by: newbie_mw | last post by:
Hi Ian, Thanks for help! The columns and variables are exactly matched so it shouldn't be the problem. Actually the original html and php files are pretty long. But I guess a more detailed...
1
by: newbie_mw | last post by:
Seems my post was buried in more cries for help :-) I will try again. It's probably a very novice question so please take a look! Thanks!...
32
by: Nuno Paquete | last post by:
Hi group. I'm using this code to see if is there any parameter for variable "menu": if($_GET == "downloads") .... But this code log errors if there is no parameter passed (this heappens at...
1
by: Westcoast Sheri | last post by:
When "register globals" is set to "on," has anyone noticed if PHP Version 5 affects the usage of "isset" or not? For example, in a webpage form, should the following code: if...
1
by: Michael Brennan-White | last post by:
If I submit my for using a get action the resulting page loads . If I use a post action I get an error page saying "The page cannot be found". I am calling the originating page!!! This happens...
2
by: Bob Stearns | last post by:
I thought that the given expression was always TRUE if "not_there" wasn't among the keys (or subscripts if you will) of $_SESSION. Below find a dump of $_SESSION, a small snippet of code and the...
7
by: Anette | last post by:
Hi, I'm very new at PHP. I've tried for a long time now to get a page that has a form in a certain way: 1. When form is submitted the data in a table should be writted on the same page. 2....
5
by: Pseudonyme | last post by:
Dear All : Ever had an httpd error_log bigger than the httpd access log ? We are using Linux-Apache-Fedora-Httpd 2006 configuration. The PHP lines code that lead too tons of errors are : ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.