July 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

XML Feeds

I've always wondered what the ? is

Permalink 05:18:47 pm Categories: Code & Stuff

Finally after years of php programming I now understand the use of the ? in a line of php code to define variables. It is called a tenerary operator and work like this.

QUESTION : ANSWER ? NO ANSWER

For example, in th examples, let's talk about the $normal variable that I set which will echo selected="selected" in the select menu if the user chose Normal mode in the form.

PHP Example: $normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' );

So in basic terms if the condition ($_GET['mode'] == 'normal') reutrns a TRUE result the variable $normal will be given the result of selected, otherwise a FALSE answer will assign $normal '' for no string.

I found this at phpfreaks while going through their excellent tutorial on building a weighted php/mysql search engine.

Comments, Pingbacks:

No Comments/Pingbacks for this post yet...

This post has 1 feedback awaiting moderation...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))