newbie syntax question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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?


Re: newbie syntax question

Quoted text here. Click to load it


print directive does not return a string expression only write it. If
it has write the output, it returns TRUE.

Quoted text here. Click to load it

Bu imza Süper İnek Güçlerine sahip değil

Cafer 'cfb' Şimşek

Re: newbie syntax question


Two things really:

1) The period in the second line you post should have been a semi-colon
to seperate the two commands. Alternatively you could have omitted the
second "print".

2) Be careful with "order of operations" in PHP when using the dot (.)
operator. When you use the dot operator always to brackets around any
equations you use. For example instead of:

print '12==12 : '.12==12 might instead use:

print '12==12 : '.(12==12)

...and even more correctly you would convert (12==12) to a string
before concat-ing it. I'll let you figure out the best way to do that.

Now as to figure out why your program gives the odd '112 == 12 : 1':
The first thing PHP does here is evaluate 12 == 12, converts it to a
string so that it can be passed to the print function and then it
passes that result to the print function. The result is that a '1' is
sent to output. The print function always returns true. It converts
this return value and converts it to a string to be used with the dot
(.) operator. Next it takes the string '12 == 12 : ' and appends the
string representation of the return value of the second print function.
It passes the combined string to the print function which sends '12 ==
12 : 1' to output. But the 1 on the end is not the 12 == 12 equation
result but rather print's return value! The output to the client is

112 == 12 : 1

 + The first '1' is the result of (12 == 12).
 + The '12 == 12 : ' is the string you specified.
 + The second '1' is the return value from the second print command.

To demonstrate, try this:

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

...(note the 11 instead of the 12). You get this output now:

012 = 11 : 1


Re: newbie syntax question

Quoted text here. Click to load it

I don't understand - the ( ) enforces that (12==12) is evaluated first *yet*
at the same time you say above that PHP's default action is exactly that as
well - so while adding the parens fixes it (I tested it)  I still don't
understand why - cause you state that that order of precedence is the you see what I'm getting at? If it's the default that "==" will
get eval 1st then the parans should have no effect - but they do...

And below you show that print function  returns and prints true (the 1), but
I'm not saying print(print...) ie. I'm not telling it to evaluate itself...

Too much for a sub genius like me to handle - i thought the line (arg) evals
and "prints" just send it to output Whoa...

Quoted text here. Click to load it

Whoa, that's saying that the initial argument to the print function is not
completely evaluated, but only half-way evaluated and passed and then...gosh
I'd expect the function to let the args or params - whatever - I'd expect it
to print the result of the line and the print fuction itself not to be a
factor like your saying.


Re: newbie syntax question

Sorry, my response wasn't very clear. Your code was:

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

As you can see, you are passing a call to print to another call to
print. That is -- you are passing print to print. If you remove the
second print like so:

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

The problem might go away but you run the risk of having all kinds of
"order of operations" issues. So as a general rule, I always put
brackets around equations used with the dot operator like so:

print    '12 == 12 : ' .(12 == 12);

As you said: the brackets may be redundant (and probably are) but I do
this out of habbit now because I have been bit my this before and
debugging order of operations bugs can be a pain. In any case I think
the habbit is a good one anyways.


Re: newbie syntax question

Hash: SHA1

leegold wrote:

Quoted text here. Click to load it

So, let's have a look at the operator precedence table* to explain what's
happening behind the scenes while evaluating this:

print '12==12' . print (12==12);


First of all, the outer "print" wants to be evaluated. In order to do so,
the expression it refers to ("'12==12' . print (12==12)") has to be

The expression "'12==12' . print (12==12)" wants to be evaluated. That
expression can be split into two expressions, "'12==12'" and "print
(12==12)", separated by the left-associative binary operator "." (string
concatenate). So, in order to evaluate this expression, first I'll have to
evaluate "'12==12'", then evaluate "print (12==12)", then apply the binary

So, the expression "'12==12'" wants to be evaluated. As it is a constant, it
gets evaluated as such.  

Next, the expression "print (12==12)" wants to be evaluated. And here comes
the trick. You are not evaluating a numerical expression, but a function
that does I/O (outputs something). But, in order to evaluate this
print(12==12), first we have to evaluate (12==12).

So, (12==12) gets evaluated, as (bool) true.

Then, "print ((bool) true)" is ready to run. This outputs "1" by stdout
(implicit type casting from bool to string), and the expression "print
(12==12)" gets evaluated as (bool) true, as print has been sucessful.

Then, "'12==12' . print(12==12)" is ready to be evaluated. At this time,
that expression equals "'12==12' . (bool) true". So, (bool) true gets
implicitly casted from bool to string, and both strings are concatented.
Now that expression equals "12==121".

Then, the outer print is ready to be run and evaluated. While running, it
outputs "12==121" by stdout. Then, it evaluates to (bool) true.

If that was too much for you, just think about the following code:


$a = '12==12: ';
$b = print (12==12);
print $a.$b;


$a is evaluated first. Then $b, then "print $a.$b". That's how it happens.

Quoted text here. Click to load it

They are not "factors". As in C (and several other imperative languages),
PHP works with expressions.

- --  
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net ; ;
Version: GnuPG v1.4.2 (GNU/Linux)


Re: newbie syntax question

why print not echo?

my favorite question (for students)
echo "$text==$num ".($text==$num);
echo "<br>$text===$num ".($text==$num);

Re: newbie syntax question

Was the third equals sign left out of the end of the last line
deliberatly or a typo?


Re: newbie syntax question

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Site Timeline