You may or may not have heard of shorthand if/else statements, but they do exist, and trust me they make things SO much more ordered in your code when used in the right way.
This is the Shorthand version – below this, if you cannot work it out for yourself is a nice description.
Description of how this actually works
We all (i would hope) know the normal way of writing if/else statements (here is a recap just incase)
That sort of statement is great for in the body of your code, but when want a dynamic selector within your HTML code, it gets very messy.
EG:
Now – that is alot to write and will make your HTML form look a total mess, this is where the shorthand comes in. It allows us to write the same thing inline
Look:
As you can see – there is a huge difference in the amount of code – and if Wordpress formatted it nicely – it would look loads better!
And the all important Explanation!
So – $myGender is the variable we are checking, in this case we are checking to see if $myGender == ‘male’
The questionmark (?) seperates the ‘answers’.
The first ‘answer’ is what is set/displayed when the query is true, the second is if the query is false.
This is the equilivent to :
Hope that helps some of you out!
11 Responses for "Shorthand PHP IF/ELSE Statements = much shorter code!"
Awesome! I’ve been learning C and wondered if PHP allowed conditionals like C does, since they share the same syntax (for the most part.)
I love your calendar, by the way. Awesome stuff there.
=)
just what i needed for a project i’m working on, it’s not like you can just code it from scratch, it’s allways great and saves time to have the basic structure to work with.
Shorthand makes for such easier reading of code.
One could only wish that everyone stuck to a standard a such.
Ever had to endure someone else horrid coding style and got to the point where you thought your eyes were going to bleed? Yea, definitely not good.
You have some nice jquery stuff on here. Consider yourself bookmarked!
Thx,
R.
Thanks, I have used conditional operator in C many times but not in PHP. Now I’ll try it. It’s really save space and time.
This is just what I was searching for – thanks for explaining it. I want to shorten my code as MUCH as possible, and while it may only shave a few microseconds off processing time to use shorthand like this, if you’re running hundreds of processes a day – all those microseconds add up.
this is fantastic
Hi, do you have a little error after “And the all important Explanation!”, did you write:
echo ($myGender = ‘male’ ? ‘ selected’ : ”);
and should be:
echo ($myGender == ‘male’ ? ‘ selected’ : ”);
Beacuse $myGender = ‘male’ ever is true and ever you got ’selected’. Have a nice day =)
[...] via Shorthand PHP IF/ELSE Statements = much shorter code! – Nodstrum [...]
Dear Jamie
Got ur callendar code and was trying to run it but getting the following errors
Notice: Undefined index: nodstrumCalendarV2 in C:\wamp\www\mysqlCalendar_1_2\rpc.php on line 89
Login
Mon
Tue
Wed
Thur
Fri
Sat
Sun
Notice: Undefined index: nodstrumCalendarV2 in C:\wamp\www\mysqlCalendar_1_2\rpc.php on line 143
Notice: Undefined variable: tTop in C:\wamp\www\mysqlCalendar_1_2\rpc.php on line 172
1
plz try and help
Can this code be extended beyond a single condition check?
Need to include selected=”selected” to be valid markup
<input type='radio' name='gender' value='m' >Male
<input type='radio' name='gender' value='f' >Female
Also, this one is useful for forms checking if value is set or not
$rad = isset($_POST['rad'])?$_POST['rad']:NULL;
Leave a reply