Page 1 of 1

Problem getting results from Magic Constants

Posted: Mon Jun 29, 2009 1:34 pm
by JohnE1
O/S: Ubuntu 9.04 Jaunty Jackalope
PHP version (from phpinfo): PHP Version 5.2.6-3ubuntu4.1

Hi all, I'm a noobie to PHP. I'm going through a book to learn PHP and do not know why this simple example is not working. I've Googled on magic constants and find plenty of examples like the one I already have.

<?
echo "This is file "._FILE_ ;
echo "This is line number "._LINE_ ;
<?>

Output:

This is file _FILE_
This is line number _LINE_

Why isn't the code above working as expected? Is there a configuration setting I need to change for these "magic constants" to work?

TIA!

John

Re: Problem getting results from Magic Constants

Posted: Mon Jun 29, 2009 1:36 pm
by VladSun
http://us2.php.net/manual/en/language.constants.php

look again ;)

__FILE__

PS: String concatenation in PHP is done by using "." operator:

Code: Select all

echo "Variable A has value of ".$a;

Re: Problem getting results from Magic Constants

Posted: Mon Jun 29, 2009 1:53 pm
by JohnE1
Thanks, but that didn't fix it. The original code example in the book had the dots, I just left them out by mistake in my forum message for help.

I have tried using the dots with no space around them and with a blank space on each side of them, and the results are still the same... the code doesn't work as it should.

Is there something else that can prevent the magic constants from working?

I'm thinking there's a configuration setting I need to change. Is there something on phpinfo()'s output you'd like me to check?

John

Re: Problem getting results from Magic Constants

Posted: Mon Jun 29, 2009 2:23 pm
by JohnE1
There was more code to the example, so I decided to add it and test it.
To my surprise, the new lines (with constants that begin with "PHP_") worked!

Code: Select all

 <?
echo "This file is "._FILE_; 
echo "<br>This is line number "._LINE_;
echo "<br>I am using ".PHP_VERSION;
echo "<br>This test is being run on ".PHP_OS;
?>
 
This file is _FILE_
This is line number _LINE_
I am using 5.2.6-3ubuntu4.1
This test is being run on Linux 
Thanks for any help in solving this mystery.

John

Re: Problem getting results from Magic Constants

Posted: Mon Jun 29, 2009 2:35 pm
by requinix
VladSun wrote:http://us2.php.net/manual/en/language.constants.php

look again ;)

__FILE__

Re: Problem getting results from Magic Constants

Posted: Mon Jun 29, 2009 2:36 pm
by pickle
The magic constants are:

[underscore][underscore]FILE[underscore][underscore], not

[underscore]FILE[underscore]

Re: Problem getting results from Magic Constants

Posted: Mon Jun 29, 2009 3:36 pm
by JohnE1
Thank you, thank you, guys!! :D

Double underscore.... WOW!!

I went back and looked at the book... and it looks like the book's publisher made the very same mistake, because the underscores around FILE and LINE look the same length as the one after PHP_ , and author never bothered to point out the importance of 2 underscores. I don't feel so blind now, but I can use old age as an excuse. LOL

John