Little known PHP Idioms etc.

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

onion2k wrote:I nest ternary statements.
I do too :oops:

but I try to do it like this:

Code: Select all

$var =
    ($expression)
    ? true :
    ($expression)
    ? true :
    false
so it's at least a little less confusing
mikesmith76
Forum Commoner
Posts: 34
Joined: Fri Aug 25, 2006 7:10 am
Location: Manchester, UK

Post by mikesmith76 »

I nest ternary statements.
What's wrong with nesting them, apart from making the code a little bit more difficult to read?

I find myself using them all the time with things like

Code: Select all

$var = (isset($_GET['var']) && ctype_digit($_GET['var']))?$_GET['var']:false;
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Personally, I find that hard to read.

What's wrong with:

Code: Select all

if(!isset($_GET['var']) || !is_numeric($_GET['var'])) $_GET['var'] = false;
I used is_numeric instead of ctype_digit because I've never heard of the latter and cannot see the difference.

EDIT | Ok, ctype_digit means every character in the string must actually be a digit, whereas is_numeric has support for hexadecimal numbers and what not. Still would go with is_numeric because it's alot clearer what the function does.
mikesmith76
Forum Commoner
Posts: 34
Joined: Fri Aug 25, 2006 7:10 am
Location: Manchester, UK

Post by mikesmith76 »

Don't think there's anything wrong with either approach, just down to personal preference. Got used to using the ternary style but I suppose it doesn't make any difference either way.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Other than the fact there are a few more lines involved, why would you not want to do this:

Code: Select all

<?php
if ( isset($_GET['var']) && ctype_digit($_GET['var']) ) {
    $var = $_GET['var'];
} else {
    $var = false;
}
?>
instead of this:

Code: Select all

<?php
$var = (isset($_GET['var']) && ctype_digit($_GET['var']))?$_GET['var']:false;
?>
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Everah wrote:Other than the fact there are a few more lines involved
That's the only reason. Personally, I think my solution is the most readable and line-friendly :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Line-friendly? :roll:

More readable for who?

I try to write my code so newbs can read it and follow the logic.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

feyd wrote:Line-friendly? :roll:

More readable for who?

I try to write my code so newbs can read it and follow the logic.
Couldn't think of a good phrase in place of line-friendly! :) Y'know what I mean.

More readable for newbs, but the more lines you have, the more readable it is. So you have to strike a happy medium, which I think my code does. In my opinion.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Couldn't think of a good phrase in place of line-friendly! Smile Y'know what I mean.
'fraid I don't. Who cares about lines of code anyway?

You don't actually need ternary for anything you know, its just a shorthand. I find myself using it on occasion most of the time I can force myself to use an if..else and it looks better for it when I can't force myself to use if..else is usually because I'm feeling lazy -- which is a really :roll: great motivator for good code.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

feyd wrote:I try to write my code so newbs can read it and follow the logic.
Amen to that brother!

Readable code, for me, is understandable code. Same as commenting, in my opinion. What the code does not explain clearly the comments do, but I would hope that no one looking at my code would ever look at it and scratch their head asking what that means.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

The only place I use ternary is in forms, like:

Code: Select all

<input type="text" name="name" value="<?php  echo isset($_POST["name"]) ? htmlentities($_POST["name"], ENT_QUOTES, 'UTF-8') : '0';  ?>" />
That's a tiny bit more readable then having multiple lines of an if else loop mixed in. However, there are probably better ways to do this ;)
mikesmith76
Forum Commoner
Posts: 34
Joined: Fri Aug 25, 2006 7:10 am
Location: Manchester, UK

Post by mikesmith76 »

I use them (and will continue to do so) as I don't see what is difficult to understand about them. Have never come across a need to next them tho, but i imagine that could become messy
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Using ternary style in PHP, is like using l33t speek in ur lang.

Shorter is harder to read by other people. I agree with Everah and Feyd - and I also have banned ternary and on occassion even the short single line if() condition in a coding standard. Sure longer versions of conditions take longer to type, but they often make things more obvious. In my view, if I have to slow down to decipher a piece of code I'm more likely to rewrite it on the spot to a) figure it out, and b) not have someone else down the line spend the extra seconds figuring it out. We developers are supposed to be lazy people, but sometimes that laziness can bite you in the future.
wei
Forum Contributor
Posts: 140
Joined: Wed Jul 12, 2006 12:18 am

Post by wei »

i think ternary are readable when used with simple assignements, e.g.

Code: Select all

$isMale = ... some complicated boolean expression to define a male...;
$name = $isMale ? 'Bob' : 'Jane';
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

wei wrote:i think ternary are readable when used with simple assignements, e.g.

Code: Select all

$isMale = ... some complicated boolean expression to define a male...;
$name = $isMale ? 'Bob' : 'Jane';
I agree
Post Reply