-1

First I would like to let you guys know that I am no expert in PHP, I know some basics. I am learning by myself, this is why I am turning to you guys for help.

I made some search but in vein, I can't figure out what should I search for.

Line of code from a whole block

$base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';

So the block of code is for building the url. This line of code choose if its either http or https.

(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')

This line checks if https is set. Until now its ok

 ? 'https' : 'http';

This is where I need help. What does the '?' and ':' does?

Thanks in advance. Also it would be great if you guys could recommend me some books, sites or tutorials where I can learn.

Kind Regards

4 Answers4

0

It is the ternary operator.

It returns the value between ? and : if the condition in front of the ? is true. And the value after the : if the condition is false.

I recommend reading the PHP manual in general, it gives lots of information on almost any topic regarding PHP.

TimWolla
  • 28,958
  • 8
  • 59
  • 84
  • thanks. but there are a lot of stuff in it, should I read the whole documentations? – Ouzayr Khedun Jun 20 '13 at 12:05
  • @OuzKedz You should read the [language reference](http://www.php.net/manual/en/langref.php) at least. It gives information on basic use. It is probably impossible to read the whole function documention, I recommend searching for a proper method in case you need one and reading the appropriate manual afterwards. – TimWolla Jun 20 '13 at 12:08
  • @OuzKedz - I suggest you read it as you need it. But the [Language Reference](http://www.php.net/manual/en/langref.php) chapter is worth an afternoon, esp. from "Basic syntax" to "Control Structures". – Álvaro González Jun 20 '13 at 12:10
0

It's shorthand for if - else combo

statement ? code 1 : code 2

is equivalent to

if( statement ) { code 1 } else { code 2 }

http://www.php.net/manual/en/language.operators.comparison.php

paranoid
  • 535
  • 2
  • 11
0

This is ternary conditional operator: http://php.net/manual/en/language.operators.comparison.php

Rafał Malinowski
  • 996
  • 1
  • 6
  • 8
0
$base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') 

? 'https' : 'http';
this is like if else statement if above line is true then $base_root will assigned with 'https'(which is just after ?) if it is false then it will get assigned with 'http'(which is after :)

just check below condition you will get understand easily...

if((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')){
    $base_root = 'https';
}else{
    $base_root = 'http';
}
Shiv
  • 69
  • 3