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

Assigning variables in if's

Hi!

In while (false !== ($file = readdir($handle)))

what happens?
The $file gets assigned, and the value of that is compared with false?
The assignment is being compared with false

Say, I want to return true or an error code. Can I do;

if($err = somefunc())
echo "ok"
else
echo "error: $err";

WBR
Sonnich
Nov 28 '07 #1
12 1230
On 28 Nov, 09:24, jodleren <sonn...@hot.eewrote:
Hi!

In while (false !== ($file = readdir($handle)))

what happens?
The $file gets assigned, and the value of that is compared with false?
The assignment is being compared with false

Say, I want to return true or an error code. Can I do;

if($err = somefunc())
echo "ok"
else
echo "error: $err";

WBR
Sonnich
if($err = somefunc())
echo "ok"
else
echo "error: $err";

may work.

An assignment "returns" the value that was assigned. This is why you
can do:

$a = $b = $c = 5;

and all 3 variables will have the value 5.

In your "if" comparison, any non-empty or non-zero value in the
brackets will be taken to be true.

So, if somefunc() returns 0 or nothing for a good result, then your
statement will do what you want.
Nov 28 '07 #2
On Nov 28, 12:35 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 28 Nov, 09:24, jodleren <sonn...@hot.eewrote:


Hi!
In while (false !== ($file = readdir($handle)))
what happens?
The $file gets assigned, and the value of that is compared with false?
The assignment is being compared with false
Say, I want to return true or an error code. Can I do;
if($err = somefunc())
echo "ok"
else
echo "error: $err";
WBR
Sonnich

if($err = somefunc())
echo "ok"
else
echo "error: $err";

may work.

An assignment "returns" the value that was assigned. This is why you
can do:

$a = $b = $c = 5;

and all 3 variables will have the value 5.

In your "if" comparison, any non-empty or non-zero value in the
brackets will be taken to be true.

So, if somefunc() returns 0 or nothing for a good result, then your
statement will do what you want.- Hide quoted text -

- Show quoted text -
If found, that it does not work, so I ended up using this

$err = somefunc();
if($err===true)
echo "ok"
else
echo "error: $err";

and probably I'll change it to return 0 as no error.

WBR
Sonnich
Nov 29 '07 #3
jodleren wrote:
$err = somefunc();
if($err===true)
echo "ok"
else
echo "error: $err";
echo (true === ($err = somefunc())) ? 'ok' : "error: $err";

:-)

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 17:00.]
[Now Playing: Counting Crows - Walkaways]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Nov 29 '07 #4
Toby A Inkster wrote:
jodleren wrote:
>$err = somefunc();
if($err===true)
echo "ok"
else
echo "error: $err";

echo (true === ($err = somefunc())) ? 'ok' : "error: $err";

:-)
or more readable(to me) IMHO
$err=foo();
switch($err){
case true: echo 'ok';break;
case false: echo 'error: '.$err;
}

But of course thats just a matter of style.
Nov 29 '07 #5
taps128 wrote:
Toby A Inkster wrote:
>jodleren wrote:
>>$err = somefunc();
if($err===true)
echo "ok"
else
echo "error: $err";

echo (true === ($err = somefunc())) ? 'ok' : "error: $err";

:-)
or more readable(to me) IMHO
$err=foo();
switch($err){
case true: echo 'ok';break;
case false: echo 'error: '.$err;
}

But of course thats just a matter of style.
More than a matter of style. If the function returns '1', your way will
print 'ok', while Toby's function will return the more correct 'error: 1'.

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

Nov 29 '07 #6
taps128 wrote:
or more readable(to me)
Readable perhaps, but boring. echo (true === ($err = somefunc())) ? 'ok' :
"error: $err"; is fun. It invites the reader to explore its mysteries; to
embark in a journey of discovery, a quest filled with a sense of awe and
wonder. And in a sense it helps the reader to uncover new things about
*themselves* and unfold the secrets of their very souls.

Plus it saves almost 30 bytes.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 20:07.]
[Now Playing: Keane - On a Day like Today]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Nov 29 '07 #7

Toby A Inkster <us**********@tobyinkster.co.ukwrote in
<p0************@ophelia.g5n.co.uk>:
taps128 wrote:
>or more readable(to me)

Readable perhaps, but boring. echo (true === ($err =
somefunc())) ? 'ok' : "error: $err"; is fun. It invites
the reader to explore its mysteries; to embark in a
journey of discovery, a quest filled with a sense of awe
and wonder.
This particular example is very, very mild, and would be
just fine with me, but as a general rule, fun code invites
the code reviewer to explore the mysteries of the
commiter's entrails. The reason for that is that when we
need something done, journey of discovery is the last thing
we want, especially if we can only spare the least
experienced member of the team to work on the task at hand.
Plus it saves almost 30 bytes.
Whoa, groovy. It's, like, gonna save seven and a half baby
seals due to its, like, sheer, mind-boggling environment
friendliness.

--
....also, I submit that we all must honourably commit seppuku
right now rather than serve the Dark Side by producing the
HTML 5 spec.
Nov 29 '07 #8
On Nov 29, 1:22 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
taps128 wrote:
or more readable(to me)

Readable perhaps, but boring. echo (true === ($err = somefunc())) ? 'ok' :
"error: $err"; is fun. It invites the reader to explore its mysteries; to
embark in a journey of discovery, a quest filled with a sense of awe and
wonder. And in a sense it helps the reader to uncover new things about
*themselves* and unfold the secrets of their very souls.

Plus it saves almost 30 bytes.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 20:07.]
[Now Playing: Keane - On a Day like Today]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
And is completely contrary to the principles of writing maintainable
code. :) What code does should be self-evident, or at least self-
explanatory and well commented, because the poor sod who has to look
at the code in 18 months time to figure out why it's not behaving as
intended might be you.
Nov 29 '07 #9
Gordon wrote:
On Nov 29, 1:22 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
>taps128 wrote:
>>or more readable(to me)
Readable perhaps, but boring. echo (true === ($err = somefunc())) ? 'ok' :
"error: $err"; is fun. It invites the reader to explore its mysteries; to
embark in a journey of discovery, a quest filled with a sense of awe and
wonder. And in a sense it helps the reader to uncover new things about
*themselves* and unfold the secrets of their very souls.

Plus it saves almost 30 bytes.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 20:07.]
[Now Playing: Keane - On a Day like Today]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/

And is completely contrary to the principles of writing maintainable
code. :) What code does should be self-evident, or at least self-
explanatory and well commented, because the poor sod who has to look
at the code in 18 months time to figure out why it's not behaving as
intended might be you.
That is very true, and it depends on who's going to be reading it.

For instance, experienced C/C++ programmers would understand what it's
doing, even if they don't know PHP. However, a Perl programmer would be
more confused.

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

Nov 29 '07 #10
Jerry Stuckle wrote:
taps128 wrote:
>Toby A Inkster wrote:
>>jodleren wrote:

$err = somefunc();
if($err===true)
echo "ok"
else
echo "error: $err";

echo (true === ($err = somefunc())) ? 'ok' : "error: $err";

:-)
or more readable(to me) IMHO
$err=foo();
switch($err){
case true: echo 'ok';break;
case false: echo 'error: '.$err;
}

But of course thats just a matter of style.

More than a matter of style. If the function returns '1', your way will
print 'ok', while Toby's function will return the more correct 'error: 1'.
You right, i missed those '==='
Nov 29 '07 #11
Gordon wrote:
On Nov 29, 1:22 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
>taps128 wrote:
>>or more readable(to me)
Readable perhaps, but boring. echo (true === ($err = somefunc())) ? 'ok' :
"error: $err"; is fun. It invites the reader to explore its mysteries; to
embark in a journey of discovery, a quest filled with a sense of awe and
wonder. And in a sense it helps the reader to uncover new things about
*themselves* and unfold the secrets of their very souls.

Plus it saves almost 30 bytes.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 20:07.]
[Now Playing: Keane - On a Day like Today]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/

And is completely contrary to the principles of writing maintainable
code. :) What code does should be self-evident, or at least self-
explanatory and well commented, because the poor sod who has to look
at the code in 18 months time to figure out why it's not behaving as
intended might be you.
The ternary operator should not be 'confusing' to any competent PHP
programmer. Don't just leave out the parts that "Aren't your style".
Because you will come unstuck one day, when you don't understand
something that is normal and quite common.

besides,
echo(true===$e=somefunc())?'ok':"error: $e";

saves even more bytes ;)

- Michael
Dec 3 '07 #12
Toby A Inkster <us**********@tobyinkster.co.ukwrote:
>
Readable perhaps, but boring. echo (true === ($err = somefunc())) ? 'ok' :
"error: $err"; is fun. It invites the reader to explore its mysteries; to
embark in a journey of discovery, a quest filled with a sense of awe and
wonder. And in a sense it helps the reader to uncover new things about
*themselves* and unfold the secrets of their very souls.
+1 QOTW

I have to write that down. That has to be both the best and the worst
justification for obfuscated code that I've ever read.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 4 '07 #13

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...
7
by: jodleren | last post by:
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...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.