473,396 Members | 2,036 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,396 software developers and data experts.

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

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
7 1758
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ben | last post by:
Hi all, This may sound easy but I'm having trouble assigning values to array element. My problem is as follows: m = for o in m: # Here first o is 'Peter'.. I want to do something like this:...
17
by: fAbs | last post by:
hi, when I do: someclass &someobject = someotherobject; it asigns the reference of someotherobject to the variable 'someobject' that you just declared. SO that basically someobject and...
2
by: KathyB | last post by:
Hi, figured out where I was going wrong in my post just prior, but is there ANY way I can assign several variables to then use them in an Update statement, for example (this does not work): ...
1
by: Dave | last post by:
I am trying to create a function in SQL 2000. Im rusty on function syntax and need to also assign some variables for use within this function. Example: create function blah (@param1 int) ...
0
by: vanGogh | last post by:
I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool. My goal is to: - write an application that can parse XML documents - fill objects (based on the generated...
4
by: Jon | last post by:
This seems strange, but maybe there's some basic concept I'm missing. When I assign one class member to another, any methods that are applied to one are applied to both variables.I can't get the...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
9
by: shortyzms | last post by:
I'm having a problem with assigning 64-bit hex values to unsigned long variables in MS VS2005 c++ compiler. unsigned long Number; Number = 0x1000000000000000UL; After this declaration and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.