Re: assigning variables in e.g. an if ....
- From: Jensen Somers <jensen@xxxxxxxxxxxxxxx>
- Date: Thu, 25 Sep 2008 11:55:39 +0200
Bill H wrote:
On Sep 25, 4:25 am, Geoff Berrow <blthe...@xxxxxxxxxxx> wrote:Message-ID:
<5f0aa7b8-0fe4-4b34-8f81-f288cb0ae...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> from
jodleren contained the following:
I have tried to use this, but failed.... and I have not found anyNot sure I understand your question. AIUI parentheses work the same way
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?
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.
It appears he is trying to do variable assignment inside the if ()
statement. Is this allowed?
Yes.
I now in actionscript this is not allowed,
and I believe in perl it is allowed and always returns true (could be
wrong there).
I am not familiar with Perl, but I would assume that it would behave in a similar way to almost any other programming language. The returned value will be checked within the condition.
If so (or even if not) does a variable assignment return a true /
false value?
Bill H
The line:
if (($i = strpos($line, '=')) !== FALSE) {...}
is the equivalent of:
$i = strpos($line, '=');
if ($i !== FALSE) {...}
It's a short way of writing something and almost any programming language allows basic assignments like that. It comes in handy when for example working with while loops.
function foo($bar) {
if (is_numeric($bar)) {
return TRUE;
}
my_custom_report_error_function(
'The value %s is not a numerical value.',
$bar
);
return FALSE;
}
// Read in number from user
while (($numeric = foo($bar)) == TRUE) {
// Perform action on number and read in next character.
}
- Jensen
--
Jensen Somers <http://jsomers.eu>
Email: -http:// +jensen@
"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe trying to
produce bigger and better idiots. So far, the Universe is winning."
- Rick Cook, The Wizardry Compiled
.
- Follow-Ups:
- Re: assigning variables in e.g. an if ....
- From: Bill H
- Re: assigning variables in e.g. an if ....
- References:
- assigning variables in e.g. an if ....
- From: jodleren
- Re: assigning variables in e.g. an if ....
- From: Geoff Berrow
- Re: assigning variables in e.g. an if ....
- From: Bill H
- assigning variables in e.g. an if ....
- Prev by Date: Re: assigning variables in e.g. an if ....
- Next by Date: Re: When would you use an abstract class and when an interface?
- Previous by thread: Re: assigning variables in e.g. an if ....
- Next by thread: Re: assigning variables in e.g. an if ....
- Index(es):
Relevant Pages
|