473,800 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
12 1253
Jerry Stuckle wrote:
taps128 wrote:
>Toby A Inkster wrote:
>>jodleren wrote:

$err = somefunc();
if($err===tr ue)
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...@t obyinkster.co.u k>
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**********@t obyinkster.co.u kwrote:
>
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
8281
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: Peter = 10
17
4709
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 someotherobject are really the same variable with two names. but if I do: someclass someobject;
2
6659
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): ALTER PROCEDURE dbo.UpdateXmlWF ( @varWO varchar(50) ) AS DECLARE @varCust VARCHAR(50)
1
1952
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) declare @var1 int, @var2 int --it doesnt like this method of assigning values to my
0
1602
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 classes) with the XML data in element nodes or attributes - assemble the objects so they are contained in their parent - add the resulting object to an arrayList The problem I’m having is that I am not sure how to go about assigning an object to...
4
1258
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 code below to work (display sum, product and quotient) without re-initializing z1 each time. What's the problem here? Dim z0 As Complex = New Complex(CDbl(Me.TextBox1.Text), CDbl(Me.TextBox2.Text)) Dim z1 As Complex = New...
20
7016
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 = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
8
4547
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 "this" to the result, but I always get the error message, "Cannot assign to '<this>' because it is read-only." I've been searching on the Internet, and I notice some C# code is violating this rule. Perhaps their code is wrong.
7
1775
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 tried to use this, but failed.... and I have not found any proper information about this? E.g., if I am right, then
9
16064
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 assignment, the value in Number is always 0. However, if the hex value is 0x0000000000000001, then the value is 1 as expected. It seems like the compiler is truncating the most significant 32 bits. How can I make this work?
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10507
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10279
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10036
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.