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



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 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.


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
.



Relevant Pages

  • Re: "Dollhouse" = Duh-house
    ... He ripped through the Dollhouse, sparing Echo ... emergence of Echo as a character. ... original personality doesn't make it so. ...
    (rec.arts.tv)
  • Re: "Dollhouse" = Duh-house
    ... He ripped through the Dollhouse, sparing Echo ... emergence of Echo as a character. ... original personality doesn't make it so. ...
    (rec.arts.tv)
  • Re: Need line count explanation help
    ... currently involved in discussing with doctors wife as my bill was higher ... >> doing my margins and doing gross lines. ... >> She still is horrified I count every character. ... I said I DO BILL LINES but I figure by characters not gross ...
    (sci.med.transcription)
  • Re: trying to recursively get the files owners and permissions as well as an md5sum of the data
    ... memory taxing", which are both measurable, but "better" because md5sum is ... The NUL character is the one character that cannot ... xargs -0 tells xargs to split it's ... Here, as the cmd is not provided, xargs calls the "echo" command ...
    (comp.unix.shell)
  • Re: "Dollhouse" = Duh-house
    ... He ripped through the Dollhouse, sparing Echo ... emergence of Echo as a character. ... original personality doesn't make it so. ...
    (rec.arts.tv)