Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New syntax help

Status
Not open for further replies.

leegold2

Technical User
Oct 10, 2004
116
Note I posted this on a PHP newsgroup and I did get a detailed response from another poster. But I was a bit shocked - it seems that there's a bundle of what seems to me extra functionality in the print function. "Print()" just doesn't send the evaluated argument to output - it does all sorts of extra stuff... could someone try to explain this? I see it as mysterious at this point...



Hi,

print '12 == 12 : '; print 12 == 12;

this works, I get:

12 == 12 : 1

but I tried to shorten it:

print '12 == 12 : ' . print 12 == 12;

I get:

112 == 12 : 1

What's that extra "1" ( meaning true I assume) at the front of the output?

Thanks

 
New in the subject should be Newbie
 
This is not unexpected behavior. Here's that I think is happening.

If you check the documented definition of print at , you will see that print returns an int. Also, the description of the function reads, "Outputs arg . Returns 1, always."


When the parser gets to the line:

print '12 == 12 : ' . print 12 == 12;

it knows it needs to print something. While trying to figure out what to print, it learns it has to print the value of the concatenation of two things (specifically, the part "'12 == 12 : '" and the part "print 12 == 12"). In order to perform the concatenation, both parts must be evaluated. The first part is just a string, so that's easy -- it evaluates to the string itself. The second part is includes a function reference, so the function must be run to see what value it returns.

In order to know what to output in the second print invocation, the parser must first evaluate the statement "12 == 12". This statement evaluates to TRUE. The boolean value TRUE cannot be output, so the value is converted to an integer 1. (There's your first 1) Print() then returns the value 1, which we know it always does from the definition of the function.

The parser then performs the concatenation, converting integer 1 to string 1, then outputting the concatenated string.

The returned value of 1 from the first print() invocation in the line is discarded.



The first version:

print '12 == 12 : '; print 12 == 12;

is simply two statements and can be written as:

print '12 == 12 : ';
print 12 == 12;

The returned values of 1 from both print statements are discarded.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top