开发者

Usage of ||, OR in PHP

开发者 https://www.devze.com 2022-12-22 08:31 出处:网络
I have the following code which redirect pages depends on $path. ... $path = $this->uri->segment(3);

I have the following code which redirect pages depends on $path.

...
$path = $this->uri->segment(3);
$pathid = $this->uri->segment(4);
if($path=='forsiden'){
            redirect('','refresh');
        }elseif($path =='contact'){
            redirect('welcome/kontakt','refresh');
        }elseif($path =='illustration'){
            $this->_gallery($path,$pathid);
        }elseif($path =='webdesign'){
            redirect('welcome/webdesign','refresh');    
        }elseif($path==('web_tjenester' || 'webdesigndetails' || 
                        'vismahjemmeside' || 'joomla' || 'vismanettbutikk' || 
                        'vpasp' || 'artportfolio')){
        ...
     CODE A   
     ...    

}else{
        ...
        CODE B
        ...
                }

I am not getting right results with

$path==('web_tjenester' || 'webdesigndetails' || 
'vismahjemmeside' || 'joomla' || 'vismanettbutikk' || 
'vpasp' || 'artportfolio')

contact, illustration, gallery and webdesign are redirected and working alright. However all other pages are added CODE A.

I am expecting CODE A only when $path is web_tjenester', 'webdesigndetails', 'vismahjemmeside', 'joomla', 'vismanettbutikk', 'vpasp' or 'artportfolio'.

Could anyone point out my mistake and correct me please?

Thanks in advance.

--UPDATE--

The following works, but is there any ways shorten the code?

I am repeating ($path==..).

elseif(($path=='web_tjenester') ||开发者_如何学运维 ($path=='webdesigndetails') || 
    ($path=='vismahjemmeside') || ($path=='joomla') || ($path=='vismanettbutikk') || 
    ($path=='vpasp') || ($path=='artportfolio')){


The || operator is a logical operator. The semantics of the expression a || b is that it evaluates to true if either a or b evaluates to true, and false otherwise.

So it cannot be used in that way you intended it as the expression ('web_tjenester' || … || 'artportfolio') will be evaluated to a boolean value (to be specific: true as 'web_tjenester' evaluates to true using boolean conversion) that then will be compared to the value of $path.

If you want to test if a value is in a set of values, you can use an array and test the value with in_array:

in_array($path, array('web_tjenester', 'webdesigndetails', 'vismahjemmeside', 'joomla', 'vismanettbutikk', 'vpasp', 'artportfolio'))


$path==('web_tjenester' || 'webdesigndetails' || 
'vismahjemmeside' || 'joomla' || 'vismanettbutikk' || 
'vpasp' || 'artportfolio')

This is not valid syntax, you would need to have the $path == in front of each string separated by ORs. The easier way to do it which looks nicer is to put all your string in an array and then use the in_array() function to check.

$array = array('web_tjenester', 'webdesigndetails', 
'vismahjemmeside', 'joomla', 'vismanettbutikk',
'vpasp', 'artportfolio');
if (in_array($path, $array)) {
    // do whatever
}


because $path == ('x' || 'y') means "evaluate 'x || y', and see if that equals $path. You'd need to do

if($path == 'x' || $path == 'y')

However you may want to consider a switch statement:

switch($patch) {
   case 'web_tjenester':
   case 'webdesigndetals':
          so_and_so();
          break; 
   case 'contact':
          redirect();
          break;
}

read up on switch before doing so , however :)


You cannot do it the way you do it: You should compare everything with $path, so

$path=='web_tjenester' || $path=='webdesigndetails' || 
$path=='vismahjemmeside' || $path=='joomla' || $path=='vismanettbutikk' || 
$path=='vpasp' || $path=='artportfolio'


You can't do this

$path==('web_tjenester' || 'webdesigndetails' || 'vismahjemmeside' || 'joomla' || 'vismanettbutikk' ||'vpasp' || 'artportfolio')

It should be

$path=='web_tjenester' || $path=='webdesigndetails' || $path=='vismahjemmeside' || $path=='joomla' ...

You could set the paths you are using in an array and use [in_array()][1] for a cleaner code.

0

精彩评论

暂无评论...
验证码 换一张
取 消