473,388 Members | 1,499 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,388 software developers and data experts.

documentation help needed

I have used and tested the following:

$x = myFunction($someValue)? "true":"false";

When myFunction() returns an empty array, it results in false, and when
it returns a non-empty array it results in true. This is what I want
and expect, but I can't find any documentation on it. Being naturally
cautious (spelled paranoid) I don't want to rely on this until I'm
certain. Can anyone point me to the documentation that supports what
I'm seeing?
Jun 2 '08 #1
13 1038
William Gill schreef:
I have used and tested the following:

$x = myFunction($someValue)? "true":"false";

When myFunction() returns an empty array, it results in false, and when
it returns a non-empty array it results in true. This is what I want
and expect, but I can't find any documentation on it. Being naturally
cautious (spelled paranoid) I don't want to rely on this until I'm
certain. Can anyone point me to the documentation that supports what
I'm seeing?
Hi,

Why would an empty array evaluate to false?
I don't even know if it does, nor do I care.
Do you want your scripts to rely on such effects?
I wouldn't.

Why not simply write cleanly what you want?

// assuming myFunction always returns an array
$xTemp = myFunction($someValue);
$x = (count($xTemp) == 0) ? "false" : "true";

Or in case you need true/false as booleans and not as strings:
$xTemp = myFunction($someValue);
$x = (count($xTemp) 0);

That way somebody else reading your code has also a clue what you are
trying to accomplish.
If you write it in your way, the next person seeing your code will ask
the same questions you are asking yourself now.
That next person could even be yourself in a few years. :P
Regards,
Erwin Moller (clean-code-police)
Jun 2 '08 #2
Erwin Moller a écrit :
// assuming myFunction always returns an array
$xTemp = myFunction($someValue);
$x = (count($xTemp) == 0) ? "false" : "true";
You may check empty() which directly returns a boolean ^^

Regards,
--
Guillaume
Jun 2 '08 #3
Erwin Moller wrote:
>
Why would an empty array evaluate to false?
I don't even know if it does, nor do I care.
It's been a couple of weeks since i tested, but that's what I found.
Do you want your scripts to rely on such effects?
I wouldn't.

Why not simply write cleanly what you want?

// assuming myFunction always returns an array
$xTemp = myFunction($someValue);
$x = (count($xTemp) == 0) ? "false" : "true";

Or in case you need true/false as booleans and not as strings:
$xTemp = myFunction($someValue);
$x = (count($xTemp) 0);

That way somebody else reading your code has also a clue what you are
trying to accomplish.
If you write it in your way, the next person seeing your code will ask
the same questions you are asking yourself now.
That next person could even be yourself in a few years. :P
Actually I am emulating someone else's code. My example was rewritten
to focus on the true/false value of the assignment operation. In
practice it reads more like:

if($errors=checkForErrors(){
/* there are errors, do something about it */
else{
/* no errors so do something else*/
}

}
Jun 2 '08 #4
Guillaume wrote:
You may check empty() which directly returns a boolean
That insures a reliable true/false test.

It also helps with a previous concern regarding the $_POST array.

Thanks.
Jun 2 '08 #5
Erwin Moller wrote:
Why would an empty array evaluate to false?
I don't even know if it does, nor do I care.
I retested and both an empty array and an assignment of an empty array
return false.

As stated elsewhere, the code I'm emulating uses the true/false result
of the assignment to control flow, but as Guillaume points out, empty()
is more explicit (and probably more reliable).
Jun 2 '08 #6
..oO(Erwin Moller)
>William Gill schreef:
>I have used and tested the following:

$x = myFunction($someValue)? "true":"false";

When myFunction() returns an empty array, it results in false, and when
it returns a non-empty array it results in true. This is what I want
and expect, but I can't find any documentation on it. Being naturally
cautious (spelled paranoid) I don't want to rely on this until I'm
certain. Can anyone point me to the documentation that supports what
I'm seeing?

Why would an empty array evaluate to false?
I don't even know if it does, nor do I care.
It does by definition, see PHP's type juggling. All these values
evaluate to FALSE if casted to boolean:

0, '0', array(), '', NULL
>Do you want your scripts to rely on such effects?
I wouldn't.
I wouldn't either usually, but for another reason. An explicit test is
cleaner and more readable code.

Micha
Jun 2 '08 #7
Michael Fesser wrote:
>
It does by definition, see PHP's type juggling. All these values
evaluate to FALSE if casted to boolean:

0, '0', array(), '', NULL
Thanks, that's the type of documentation I was looking for.
>
>Do you want your scripts to rely on such effects?
I wouldn't.

I wouldn't either usually, but for another reason. An explicit test is
cleaner and more readable code.
Agreed. Using an explicit call to empty() is clearer, but sometimes
using a multi step to feed a conditional may be counter productive.

function setClass($fieldname){
global $form_errors;
if(isset($form_errors)) {
return array_key_exists('$fieldname', $form_errors) ? 'error' :
'normal'; /*set css class */
}
else{return 'normal';}
}
....
....
....

<input name="First_Name" type="text"
class="<?php echo setClass('First_Name') ?>"
value="<?php echo getVal('First_Name') ?>" size="20">


Jun 2 '08 #8
William Gill wrote:
>
function setClass($fieldname){
global $form_errors;
if(isset($form_errors)) {
return array_key_exists('$fieldname', $form_errors) ? 'error' :
'normal'; /*set css class */
}
else{return 'normal';}
}
...
...
...

<input name="First_Name" type="text"
class="<?php echo setClass('First_Name') ?>"
value="<?php echo getVal('First_Name') ?>" size="20">
Disregard my example, it is completely off base (I was distracted / had
my head up my... well, you know what I mean).
Jun 2 '08 #9
On May 26, 2:50*pm, William Gill <nore...@example.comwrote:
I have used and tested the following:

$x = myFunction($someValue)? "true":"false";

When myFunction() returns an empty array, it results in false, and when
it returns a non-empty array it results in true. *This is what I want
and expect, but I can't find any documentation on it. *Being naturally
cautious (spelled paranoid) I don't want to rely on this until I'm
certain. *Can anyone point me to the documentation that supports what
I'm seeing?
That's very odd behavior you're getting there, I was under the
impression that empty arrays evaluated to true! For that reason, I'd
consider being a bit more thorough in checking what you get back from
your function to make sure it really is what you think it is. I could
be mistaken on that front of course, I'd have to check the
documentation on what evaluates to true and what doesn't, but
personally I'd be running count() on your function's return value and
checking it was greater than 0.
Jun 2 '08 #10
In our last episode, <Do******************************@wideopenwest.com >,
the lovely and talented William Gill broadcast on comp.lang.php:
I have used and tested the following:
$x = myFunction($someValue)? "true":"false";
When myFunction() returns an empty array, it results in false, and when
it returns a non-empty array it results in true. This is what I want
and expect, but I can't find any documentation on it.
'Comparison Operators' in the "Operators" section of the manual -- Chapter
15 in my version. Or simply grep the manual for 'ternary operator' --- I
don't think there is another one.
Being naturally cautious (spelled paranoid) I don't want to rely on this
until I'm certain. Can anyone point me to the documentation that supports
what I'm seeing?

--
Lars Eighner <http://larseighner.com/us****@larseighner.com
Countdown: 238 days to go.
Jun 2 '08 #11
..oO(Gordon)
>On May 26, 2:50*pm, William Gill <nore...@example.comwrote:
>I have used and tested the following:

$x = myFunction($someValue)? "true":"false";

When myFunction() returns an empty array, it results in false, and when
it returns a non-empty array it results in true. *This is what I want
and expect, but I can't find any documentation on it. *Being naturally
cautious (spelled paranoid) I don't want to rely on this until I'm
certain. *Can anyone point me to the documentation that supports what
I'm seeing?

That's very odd behavior you're getting there, I was under the
impression that empty arrays evaluated to true!
It's correct behaviour, empty always arrays evaluate to FALSE.

Converting to boolean
http://www.php.net/manual/en/languag...oolean.casting

Micha
Jun 2 '08 #12
..oO(Gordon)
>On May 26, 2:50*pm, William Gill <nore...@example.comwrote:
>I have used and tested the following:

$x = myFunction($someValue)? "true":"false";

When myFunction() returns an empty array, it results in false, and when
it returns a non-empty array it results in true. *This is what I want
and expect, but I can't find any documentation on it. *Being naturally
cautious (spelled paranoid) I don't want to rely on this until I'm
certain. *Can anyone point me to the documentation that supports what
I'm seeing?

That's very odd behavior you're getting there, I was under the
impression that empty arrays evaluated to true!
It's correct behaviour, empty arrays always evaluate to FALSE.

Converting to boolean
http://www.php.net/manual/en/languag...oolean.casting

Micha
Jun 2 '08 #13
Greetings, William Gill.
In reply to Your message dated Monday, May 26, 2008, 19:16:51,
Erwin Moller wrote:
>Why would an empty array evaluate to false?
I don't even know if it does, nor do I care.
I retested and both an empty array and an assignment of an empty array
return false.
They are values that assumed as "empty" (no data) for comparison.
These are - "false" value, numerical zero, empty string, the NULL constant,
empty array.
To make sure you are using true/false test, use strict type comparison (the
=== or !== operators).
Then the

$arr = array();
if(false !== $arr)
{
print('it is not \'false\' value');
}

will show you the truth. Empty array is not the same as 'false' value.
As stated elsewhere, the code I'm emulating uses the true/false result
of the assignment to control flow, but as Guillaume points out, empty()
is more explicit (and probably more reliable).
For your specific needs probably, but make sure your functions returning
true/false results, before using them in flow control.
In some cases, you may want to return zero as valid result, and that will
ruine your empty($result) check.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 27 '08 #14

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

Similar topics

5
by: Stuart D. Gathman | last post by:
I am still wanting to produce Python standard format documentation for Python extensions I have written. I have looked at the docs that come with Python itself, but I am new to Latex, and don't...
20
by: Daniel R. Smorey Jr. | last post by:
I'm looking for a good place for Python documentation. I'm really lost on why it's so hard to find anything when it comes to me looking up a particular function in Python. My example would be the...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
0
by: Jeff Levinson [mcsd] | last post by:
I'm an architect for a very large fortune 100 company and we still struggle with the best balance. However, I use a couple of simple guidelines that have worked very well for me in almost all...
12
by: Fredrik Olsson | last post by:
Hello. For Java there is javadac, for Obj-C headerdoc2html, for C doxygen, and even for good old VB 6 there is VBDox. But I have found no suitable tool for documenting my .net code to get...
9
by: Aidan | last post by:
I rely heavily on MSDN for documentation when it comes to HTML/DHTML/JavaScript/CSS but as a result I often have problems getting my stuff to work in Netscape/Mozilla/Firefox. I like the MSDN...
4
by: Davíð Þórisson | last post by:
Hi, when I was programmin ASP/JScript I had a very nice utilitiy which I got from MSDN (Script56.CHM). It's simply a help file but containing object orientiated documentation of all scripting...
97
by: Cameron Laird | last post by:
QOTW: "Python makes it easy to implement algorithms." - casevh "Most of the discussion of immutables here seems to be caused by newcomers wanting to copy an idiom from another language which...
34
by: nicolasfr | last post by:
Hi, I am a bit disapointed with the current Python online documentation. I have read many messages of people complaining about the documentation, it's lack of examples and the use of complicated...
1
by: Eric Sadoyama | last post by:
I have a database documentation question, but I am not even sure how to phrase it properly so I don't know where to start looking for answers. We are developing a database that is based on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...

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.