Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

please explain this line of code (php)


  • Please log in to reply

#1
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 31,675 posts
This piece of code seems to do what I want.
It allows testing the site I'm working on over the local network.
But I have no idea what the code actually means and I resent copy&paste without understanding.

And it's not easy to Google either since it's the question mark and the :false at the end that I don't get.
Probably very easy, so go ahead. :whistling:

$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;

Thanks,
  • 0

Advertisements


#2
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts
Hi

This is a handy programming concept which i forget the name of, something like a conditional statement or operator or something.

Anyway:

$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;

($_SERVER['HTTP_HOST'] != 'localhost') is the condition which is being evaluated.

If it evaluates to true, then $domain is assigned the first value after the question mark ($_SERVER['HTTP_HOST'] in this case). If false, $domain is assigned the value after the ':' (false in this case).

It is a shorthand way of writing:

if ($_SERVER['HTTP_HOST'] != 'localhost')
	 $domain = $_SERVER['HTTP_HOST'];
else
	 $domain = false;

Very useful for when you have to obtain one of two values based on a condition.

A general version:
$var = (condition) ? (value if true) : (value if false)

Hope that helps :whistling:

jpshortstuff
  • 0

#3
Metallica

Metallica

    Spyware Veteran

  • Topic Starter
  • GeekU Moderator
  • 31,675 posts

A general version:

$var = (condition) ? (value if true) : (value if false)

Hope that helps :blink:


It sure did. Thanks for your detailed explanation. :whistling:
  • 0

#4
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts
no problemo, glad i could help :whistling:

jpshortstuff
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP