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



On Sep 25, 5:55 am, Jensen Somers <jen...@xxxxxxxxxxxxxxx> wrote:
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- Hide quoted text -

- Show quoted text -

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
.



Relevant Pages

  • Re: Client IP behind Load Balancer
    ... and the Universe trying to produce bigger and better idiots. ... So far, the Universe is winning. ... > BigIP as its default gateway. ... >> Programming today is a race between software engineers ...
    (microsoft.public.inetserver.iis)
  • Re: Which Source Code Control
    ... Hi, I just did some more testing with sourcegear vault, and it seems to do ... Programming today is a race between software engineers striving to build ... bigger and better idiots. ... So far, the Universe is winning. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: extra line drawn to blob
    ... bigger and better idiots. ... So far, the Universe is winning. ... > Programming today is a race between software engineers striving to build ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Save lines drawn in the picturebox as an lmage
    ... bigger and better idiots. ... So far, the Universe is winning. ... >> Greetz Peter ... >> Programming today is a race between software engineers striving to build ...
    (microsoft.public.dotnet.languages.vb)
  • Re: messagebox "red x" corner
    ... 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. ...
    (microsoft.public.dotnet.languages.vb)