By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

assigning variables in e.g. an if ....

P: n/a
Hi

I have been looking into php.net, but could not find any proper
description.
There is a way of assigning variables from functions, while at the
same time using them in e.g. an if.
I have tried to use this, but failed.... and I have not found any
proper information about this?

E.g., if I am right, then

if(strpos($line, '='))

could be

if( ($i=strpos($line, '=')) !== false )
echo $i;

but just how does this work? the parantheses do it?
I asume I have to understand that as a statement in a statement?

WBR
Sonnich

Sep 25 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Message-ID:
<5f**********************************@59g2000hsb.g ooglegroups.comfrom
jodleren contained the following:
>I have tried to use this, but failed.... and I have not found any
proper information about this?

E.g., if I am right, then

if(strpos($line, '='))

could be

if( ($i=strpos($line, '=')) !== false )
echo $i;

but just how does this work? the parantheses do it?
I asume I have to understand that as a statement in a statement?
Not sure I understand your question. AIUI parentheses work the same way
they do in mathematics, ie the code inside is evaluated first. The
manual tells you what a function returns (note: some functions don't
return anything more meaningful than true or false). That value can be
assigned to a variable.

Now the manual entry for strpos is this
"Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE." Note the warning, if the character being
searched for is at the beginning of the string it could return 0, or in
other words a non-boolean false.

So in the first example the 'if' will be executed if the character is
found,as long as the character is not at position 0. To check for that
you'd need

if(strpos($line, '=')!==false)

In the second example the bit in parentheses is evaluated first
$i=strpos($line, '=')

$i now contains the output of strpos, an integer if the character is
found, false if not.

So now the parentheses have been evaluated we effectively have:

if( $i !== false )
echo $i;

in other words $i will be echoed as long as it is not false.


--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk
Sep 25 '08 #2

P: n/a
On Sep 25, 4:25*am, Geoff Berrow <blthe...@ckdog.co.ukwrote:
Message-ID:
<5f0aa7b8-0fe4-4b34-8f81-f288cb0ae...@59g2000hsb.googlegroups.comfrom
jodleren contained the following:
I have tried to use this, but failed.... and I have not found any
proper information about this?
E.g., if I am right, then
*if(strpos($line, '='))
could be
*if( ($i=strpos($line, '=')) *!== false )
* *echo $i;
but just how does this work? the parantheses do it?
I asume I have to understand that as a statement in a statement?

Not sure I understand your question. *AIUI parentheses work the same way
they do in mathematics, ie the code inside is evaluated first. *The
manual tells you what a function returns (note: some functions don't
return anything more meaningful than true or false). *That value can be
assigned to a variable.

Now the manual entry for strpos is this
"Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE." Note the warning, if the character being
searched for is at the beginning of the string it could return 0, or in
other words a non-boolean false.

So in the first example the 'if' will be executed if the character is
found,as long as the character is not at position 0. To check for that
you'd need

*if(strpos($line, '=')!==false)

In the second example the bit in parentheses is evaluated first
$i=strpos($line, '=')

$i now contains the output of strpos, an integer if the character is
found, false if not.

So now the parentheses have been evaluated we effectively have:

if( $i *!== false )
*echo $i;

in other words $i will be echoed as long as it is not false.

--
Geoff Berrow *0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011http://slipperyhill.co.uk-http://4theweb.co.uk
It appears he is trying to do variable assignment inside the if ()
statement. Is this allowed? I now in actionscript this is not allowed,
and I believe in perl it is allowed and always returns true (could be
wrong there).

If so (or even if not) does a variable assignment return a true /
false value?

Bill H
Sep 25 '08 #3

P: n/a
On Sep 25, 10:30*am, Bill H <b...@ts1000.uswrote:
On Sep 25, 4:25*am, Geoff Berrow <blthe...@ckdog.co.ukwrote:
Message-ID:
<5f0aa7b8-0fe4-4b34-8f81-f288cb0ae...@59g2000hsb.googlegroups.comfrom
jodleren contained the following:
>I have tried to use this, but failed.... and I have not found any
>proper information about this?
>E.g., if I am right, then
*if(strpos($line, '='))
>could be
*if( ($i=strpos($line, '=')) *!== false )
* *echo $i;
>but just how does this work? the parantheses do it?
>I asume I have to understand that as a statement in a statement?
Not sure I understand your question. *AIUI parentheses work the same way
they do in mathematics, ie the code inside is evaluated first. *The
manual tells you what a function returns (note: some functions don't
return anything more meaningful than true or false). *That value can be
assigned to a variable.
Now the manual entry for strpos is this
"Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE." Note the warning, if the character being
searched for is at the beginning of the string it could return 0, or in
other words a non-boolean false.
So in the first example the 'if' will be executed if the character is
found,as long as the character is not at position 0. To check for that
you'd need
*if(strpos($line, '=')!==false)
In the second example the bit in parentheses is evaluated first
$i=strpos($line, '=')
$i now contains the output of strpos, an integer if the character is
found, false if not.
So now the parentheses have been evaluated we effectively have:
if( $i *!== false )
*echo $i;
in other words $i will be echoed as long as it is not false.
--
Geoff Berrow *0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011http://slipperyhill.co.uk-http://4theweb.co.uk

It appears he is trying to do variable assignment inside the if ()
statement. Is this allowed? I now in actionscript this is not allowed,
and I believe in perl it is allowed and always returns true (could be
wrong there).

If so (or even if not) does a variable assignment return a true /
false value?

Bill H
As with most languages that have C as a comm,on ancestor, assigning
within an IF is perfectly legal in PHP. I tend to use it a lot in
places where calling functions that can fail. For example:

if ($fileHandle = fopen ('/foo/bar/bax.txt'))
{
//...
fclose ($fileHandle)
}
else
{
// Error handler goes here
}
Sep 25 '08 #4

P: n/a
Message-ID:
<10**********************************@e39g2000hsf. googlegroups.comfrom
Gordon contained the following:
>As with most languages that have C as a comm,on ancestor, assigning
within an IF is perfectly legal in PHP.
And also very useful in while

while($row=mysql_fetch assoc($result)){

}
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk
Sep 25 '08 #5

P: n/a
"Bill H" <bi**@ts1000.usschreef in bericht
news:67**********************************@79g2000h sk.googlegroups.com...

But where does the "true" / "false" come from? Wouldn't a variable
assignment always be "true", or does the true false come from the
value of the variable after it has been assigned?

Bill H

<?php

$a=10;
$b=20;

$a=$b=5;

echo "a=$a\n";
echo "b=$b\n";

?>

Both variables are set to 5. $a is not set to true.
Try this:

<?php

$a=0;

if ($b=$a) echo "1: true\n";
if (($b=$a)==TRUE) echo "2: true\n";
if (($b=$a)===TRUE) echo "3: true\n";

if (!($b=$a)) echo "4: false\n";
if (($b=$a)==FALSE) echo "5: false\n";
if (($b=$a)===FALSE) echo "6: false\n";

$a=1;

if ($b=$a) echo "7: true\n";
if (($b=$a)==TRUE) echo "8: true\n";
if (($b=$a)===TRUE) echo "9: true\n";

if (!($b=$a)) echo "10: false\n";
if (($b=$a)==FALSE) echo "11: false\n";
if (($b=$a)===FALSE) echo "12: false\n";
?>

4: false
5: false
7: true
8: true

Sep 25 '08 #6

P: n/a
jodleren wrote:
Hi

I have been looking into php.net, but could not find any proper
description.
There is a way of assigning variables from functions, while at the
same time using them in e.g. an if.
I have tried to use this, but failed.... and I have not found any
proper information about this?

E.g., if I am right, then

if(strpos($line, '='))

could be

if( ($i=strpos($line, '=')) !== false )
echo $i;

but just how does this work? the parantheses do it?
I asume I have to understand that as a statement in a statement?

WBR
Sonnich

Sure, it works fine. The secret is that PHP, like C, C++ and Java,
treat the assignment operation ("=") as an operator. That is, $a=$b is
an expression, just as $a+$b is, and returns a value. In the case of
$a+$b, the returned value is, of course, the sum of $a and $b. For
$a=$b, it is the value of $a after the assignment. It means that an
assignment expression can be used anywhere another expression can be
used, i.e.

func($c=$a+$b).

The only thing to be cautious about is that the "=" operator has very
low precedence, so it's best to always enclose an expression using it in
parens to prevent unexpected results (as you did in your example).

P.S. A statement is one or more expressions terminated by a semicolon.

$i=strpos($line, '=') // is an expression.

$i=strpos($line, '='); // is a statement.

A minor syntactical difference, but a major difference in operation.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 25 '08 #7

P: n/a
Bill H wrote:
But where does the "true" / "false" come from? Wouldn't a variable
assignment always be "true", or does the true false come from the
value of the variable after it has been assigned?
The result of an assignment operation is the assigned value, which then gets
implicitly casted to a boolean value (because the 'if' construct expects a
boolean value).

--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Error 99: - CPU too tired to continue.
Sep 25 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.