473,788 Members | 2,719 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 #1
12 1250
On 28 Nov, 09:24, jodleren <sonn...@hot.ee wrote:
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...@y ahoo.comwrote:
On 28 Nov, 09:24, jodleren <sonn...@hot.ee wrote:


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===tru e)
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*******@attgl obal.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**********@t obyinkster.co.u kwrote 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...@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.
Nov 29 '07 #9
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.
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*******@attgl obal.net
=============== ===

Nov 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8280
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
4708
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
1601
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
1256
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
7013
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
1774
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
16063
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
9656
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
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8995
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
7519
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...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
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
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
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.