Jump to content

Must be a more effecient way


Monkuar

Recommended Posts

if ($_SERVER['PHP_SELF'] == '/pmi.php' OR $_SERVER['PHP_SELF'] == '/pmsent.php'){
			$location = 'Using Messenger';
			}else{
			$location  = '';
			}

 

I wanted to use

 

if ($_SERVER['PHP_SELF'] == '/pmi.php' OR '/pmsent.php'){
			$location = 'Using Messenger';
			}else{
			$location  = '';
			}

 

But that doesn't work, I need to check pmi.php, pmsent.php and pms.php, but I don't want to use if $_SERVER['PHP_SELF'] == BLA OR $_SERVER['php_selft'] again.. blah

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/259039-must-be-a-more-effecient-way/
Share on other sites

  Quote

I think there is no other way. May be you could use shorter variable name instead of  $_SERVER['PHP_SELF'] like

$ps = $_SERVER['PHP_SELF'];

$ps = $_SERVER['PHP_SELF'];
if ($ps == '/pmi.php' || $ps == '/pmsent.php'||...)

 

What's the difference between || and OR ?

  Quote

What's the difference between || and OR ?

 

Their precedence values.  In your particular  usage they both have the same end result.

 

If you have a list of values to check against a variable using an OR condition, you can use in_array as a cleaner method of doing it.  I find it doesn't really matter much until your at 3 or more values though.

 

$values = array('/pmi.php', '/pmsent.php');
if (in_array($_SERVER['PHP_SELF'], $values)){ 
  ...
}

 

 

  Quote

  Quote

What's the difference between || and OR ?

 

Their precedence values.  In your particular  usage they both have the same end result.

 

If you have a list of values to check against a variable using an OR condition, you can use in_array as a cleaner method of doing it.  I find it doesn't really matter much until your at 3 or more values though.

 

$values = array('/pmi.php', '/pmsent.php');
if (in_array($_SERVER['PHP_SELF'], $values)){ 
  ...
}

 

Lol! At 3 more values? :(

 

Speed wise, what do you suggest? the old fashion

 

if ($_SERVER['PHP_SELF'] == '/pmi.php' OR $_SERVER['PHP_SELF'] == '/pmsent.php'){
			$location = 'Using Messenger';
			}else{
			$location  = '';
			}

 

or

 

$values = array('/pmi.php', '/pmsent.php');
if (in_array($_SERVER['PHP_SELF'], $values)){ 
  ...
}

 

Speed wise is there any noticeable difference? This will be run with each refresh for each user on my forum.

I doubt one over the other would make any noticeable difference in speed.  It's mainly a readability thing.  You can certainly use in_array for just two values if you want.  the three or more is just kind of my personal rule as I find that is when it makes the most difference readability wise (exact number varies some on the length of the values).

 

  Quote

I doubt one over the other would make any noticeable difference in speed.  It's mainly a readability thing.  You can certainly use in_array for just two values if you want.  the three or more is just kind of my personal rule as I find that is when it makes the most difference readability wise (exact number varies some on the length of the values).

 

Topic Solved

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.