views:

144

answers:

5

I just spent 3 hours wondering why I couldn't start a session, then realised that I used:

session_start;

when I should have used:

session_start();

I received no error messages at all!

Perhaps then, I thought, it's a lazy way to differentiate functions from variables - but then remembered that it can't be as variables require a $

Can anyone tell me why parentheses are required then, and also why no error is given when they aren't used?

+8  A: 

I get the notice (Having set error_reporting to E_ALL):

Notice: Use of undefined constant session_start - assumed 'session_start' in /t.php on line 5

A function always needs the parentheses as else (as you can see) you can't tell the difference between a function and a constant. As code should be written so it can be understood by the human reading it, not allowing the reader to see if it is a constant or function call makes it that bit harder to read and debug.

I assume it is a notice rather than an error error due to backwards compatibility in that some older code tends to access arrays using unquoted strings which PHP would treat as quoted strings if there wasn't a constant with that name.

$myArray[myString] = 25;
Yacoby
Thank you, it was to determine the difference between a function and a constant.
MarkRobinson
A: 

All the languages I know or have ever used use brackets to describe functions whether they take no parameters (void) or have a parameter list (optional or not). You get used to it. Maybe it's a hangover from C with all it's function prototypes and all.

Having said that, the echo functon in PHP is a weird one as it's not quite a function but it's a statement - go figure.

Have a look at http://en.wikipedia.org/wiki/Function(computer_science)

Jujhar Singh
Jujhar, that's because echo is *not* a function! It is a language construct, in fact writing `<? echo "blablabla" ?>` is the same as `<?= "blablabla" ?>`See http://php.net/manual/en/function.echo.php
nico
+1  A: 

You can not make a difference between function and a constant if function doesn't have parantheses... But it is strange that it doesn't throw the errors...have you turned errors off in your php.ini file?

vucetica
A: 

() denotes that the function is to be called. Without it, you are able to pass the reference around. For example (PHP 5.3):

$callable = session_start; $callable();

ChrisSmith..zzZZ
Strictly speaking, this is not true. `session_start` is considered to be the string `"session_start"`, and adding parentheses after a variable that contains a string causes the string to be used as a function name to be called.
Ignacio Vazquez-Abrams
+1  A: 

The parentheses are required because PHP's language specification says so (even though there appears to be no explicit formal specification of the language to be found).

What PHP does when it encounters an unknown identifier is output an E_NOTICE error to let you know that an unknown identifier was used, and then assumes that you meant to use that identifier as a string.

<?php
$foo = unknown_identifier;
echo 'Now printing: ' . $foo; // prints 'Now printing: unknown_identifier' (PHP 5.2.6)
?>

The reason you're not seeing any error is likely because E_NOTICE errors are below your error reporting threshold. You can change this threshold using the error_reporting directive:

<?php
error_reporting(E_ALL);

$foo = unknown_identifier;
echo 'Now printing: ' . $foo;
?>

This will output the "Notice" error message as posted above by Yacoby.

Note that, misleadingly, the E_ALL error reporting directive does not in fact include all reporting levels in PHP 5.x. In PHP 5, you need to use E_ALL | E_STRICT to enable all error messages.

-- edit: beaten to the punch by many people.

voetsjoeba
+1 for E_STRICT which indeed allow the error to show!
MarkRobinson