Problem getting results from Magic Constants

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JohnE1
Forum Newbie
Posts: 4
Joined: Mon Jun 29, 2009 1:02 pm

Problem getting results from Magic Constants

Post 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
Last edited by JohnE1 on Mon Jun 29, 2009 1:46 pm, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Problem getting results from Magic Constants

Post 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;
Last edited by pickle on Mon Jun 29, 2009 2:35 pm, edited 1 time in total.
Reason: Fixing "String" spelled as "Sting"
There are 10 types of people in this world, those who understand binary and those who don't
JohnE1
Forum Newbie
Posts: 4
Joined: Mon Jun 29, 2009 1:02 pm

Re: Problem getting results from Magic Constants

Post 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
JohnE1
Forum Newbie
Posts: 4
Joined: Mon Jun 29, 2009 1:02 pm

Re: Problem getting results from Magic Constants

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem getting results from Magic Constants

Post by requinix »

VladSun wrote:http://us2.php.net/manual/en/language.constants.php

look again ;)

__FILE__
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Problem getting results from Magic Constants

Post by pickle »

The magic constants are:

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

[underscore]FILE[underscore]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
JohnE1
Forum Newbie
Posts: 4
Joined: Mon Jun 29, 2009 1:02 pm

Re: Problem getting results from Magic Constants

Post 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
Post Reply