473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Possibile bug?


function foo() { return 'Hello World!'; }
if ( $str = foo() || $str = foo() ) echo $str;

What should output this piece of code?
I think it's not normal what's outputting to me...

Let me know. :-)
Maverick

--
«Che non siete più liberi di non avere una casa con letto per dormire.
Lavorate voi - schiavi dei soldi - che non sentite la puzza della
schiavitù!» (a Mario di Monte Mario, Roma) # http://libre.netsons.org/
Mar 23 '07 #1
8 1068
On Fri, 23 Mar 2007 01:57:27 GMT, "Maverick FC"
<fr*********@NOSPAMlibero.itwrote:
What should output this piece of code?
I think it's not normal what's outputting to me...
Here it is what I'm trying to say:

function foo() { return 'Hello World!'; }

echo '<pre>';
if ( $str = foo() ) var_dump($str);
if ( $str = foo() || $str = foo() ) var_dump($str);
echo '</pre>';

Is that normal?

Thanks.
Maverick

--
«Che non siete più liberi di non avere una casa con letto per dormire.
Lavorate voi - schiavi dei soldi - che non sentite la puzza della
schiavitù!» (a Mario di Monte Mario, Roma) # http://libre.netsons.org/
Mar 23 '07 #2
fr*********@NOSPAMlibero.it says...
>
function foo() { return 'Hello World!'; }
if ( $str = foo() || $str = foo() ) echo $str;

What should output this piece of code?
I think it's not normal what's outputting to me...

Let me know. :-)
Maverick
It should (and I presume does) return 1.

I presume you just don't know why.

Geoff M
Mar 23 '07 #3
On Fri, 23 Mar 2007 13:52:14 +1100, Geoff Muldoon
<ge***********@trap.gmail.comwrote:
It should (and I presume does) return 1.
Actually, it returns bool(true).
I presume you just don't know why.
You're correct. :-) Why?

This code:

if ( $str = foo() ) var_dump($str);

returns the string - as I expected.

foo() is always - correctly - executed only once.
I just can't understand this behaviour.

Thanks.
Maverick

--
«Che non siete più liberi di non avere una casa con letto per dormire.
Lavorate voi - schiavi dei soldi - che non sentite la puzza della
schiavitù!» (a Mario di Monte Mario, Roma) # http://libre.netsons.org/
Mar 23 '07 #4
On Fri, 23 Mar 2007 02:32:12 GMT, "Maverick FC"
<fr*********@NOSPAMlibero.itwrote:
if ( $str = foo() || $str = foo() ) var_dump($str);
Ok, I got it.
I simply have to use brackets:

if ( ($str = foo()) || ($str = foo()) ) var_dump($str);

Stupid precedence! ;-)

Thank you, Geoff.

Greetings.
Maverick

--
«Che non siete più liberi di non avere una casa con letto per dormire.
Lavorate voi - schiavi dei soldi - che non sentite la puzza della
schiavitù!» (a Mario di Monte Mario, Roma) # http://libre.netsons.org/
Mar 23 '07 #5
>It should (and I presume does) return 1.
>
Actually, it returns bool(true).
>I presume you just don't know why.

You're correct. :-) Why?

This code:

if ( $str = foo() ) var_dump($str);

returns the string - as I expected.

foo() is always - correctly - executed only once.
I just can't understand this behaviour.
$str = foo() is always going to be true as you are assigning $str to foo()
if you are trying to see if what is returned by foo() is the same as $str
then you should use == or ===
Mar 23 '07 #6
Message-ID: <et**********@aioe.orgfrom peter contained the following:
>$str = foo() is always going to be true as you are assigning $str to foo()
if you are trying to see if what is returned by foo() is the same as $str
then you should use == or ===
Spoilsport...

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 23 '07 #7
On Fri, 23 Mar 2007 01:57:27 GMT, "Maverick FC"
<fr*********@NOSPAMlibero.itwrote:
>
function foo() { return 'Hello World!'; }
if ( $str = foo() || $str = foo() ) echo $str;

What should output this piece of code?
I think it's not normal what's outputting to me...
== for comparison
= for assignment
--
Locate your Mobile phone: <http://www.bizorg.co.uk/news.html>
Great gifts: <http://www.ThisBritain.com/ASOS_popup.html>
Mar 23 '07 #8
Spoilsport...
lol sorry did I spoil your fun
Mar 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Bralic | last post by:
Hello, I writed small graphical editor for probabilystic networks in JAVA, but there was problem with making save file inJAVA. I thinked about it and I concluded that Save in JAVA is great...
1
by: ernix | last post by:
nessuna sappia dirmi come convertire un database mysql in sql server? Avrei bisogno di visionare graficamnete le tabelle e le loro relazioni. Qualcuno può darmi una mano? Grazie infinite
15
by: Davide R. | last post by:
Ciao a tutti, vi spiego il mio problema Ho una pagina HTML che referenzia un CSS esterno. Ho alcuni elementi HTML che appartengolo ad una classe (chiamiamola "class1"). Avrei la necessità,...
2
by: teo_teo_teo | last post by:
ciao, ho bisogno di definire una variabile in xslt in modo tale in modo tale da poterla usare in un ciclo for-each assegnandole di volota in volta un differente valore.. é possibile fare...
1
by: Ghido | last post by:
Hi all, i'm writing a software with python and wxpython for manage the quality/environmental/security system for the my factory. I want to separate the gui structure from the events and database...
2
by: LaGuna | last post by:
Se si come? Ciao by Enzo
3
by: Vincenzo Milazzo | last post by:
Ciao a tutti ho una master page costituita da due sezioni, a sinistra carico ondemand un asp.net treeview impostando programmaticamente la proprietà NavigateUrl, a destra carico le content page...
9
by: Luca | last post by:
I've create a portal, now I would like to use the DNN forum module in my website (without use/install DNN) ... is it possible?? How Can I do this?? Thanks
2
by: Joey | last post by:
Hi, we have a asp.net project which is was built with .net 1.1 and would like to keep it that way. however we would like to see if we can use new class libraries built with .net v2.0 by...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.