working with php if statement -


i have following php if statements

<?php if ($crs_cat1 == "business"){   $catimage ="businessimage.png"; } else if ($crs_cat1 == "leadership") {   $catimage = "leadershipimage.png";  }  else if ($crs_cat1 == "media , design") {   $catimage = "mediadesignimage.png";  }  else if ($crs_cat1 == "web development") {   $catimage = "webdevelopmentimage.png";  }  else if ($crs_cat1 == "mobile development") {   $catimage = "mobiledevelopment.png";  }  else if ($crs_cat1 == "project management") {   $catimage = "business5.png";  }  else if ($crs_cat1 == "databases , business intelligence") {   $catimage = "businessanalysis.png";  }  else if ($crs_cat1 == "it service management") {   $catimage = "itservice.png";  }  else if ($crs_cat1 == "business analysis , agile") {   $catimage = "businessanalysis.png";  }  else if ($crs_cat1 == "network security , os") {   $catimage = "networksecurity.png";  }  else if ($crs_cat1 == "virtualization , cloud computing") {   $catimage = "virtualization.png";  }  else if ($crs_cat1 == "software quality & testing tools") {   $catimage = "testing.jpg";  }  else if ($crs_cat1 == "microsoft") {   $catimage = "microsoft2.png";  }  else if ($crs_cat1 == "%adobe%") {   $catimage = "adobe.png";  }  else if ($crs_cat1 == "ibm") {   $catimage = "ibm.jpg";  }  else {     $catimage = "negotiation.png";   }       ?> 

in if statements more if business or leadership, in sense result doesn't have exact has similar enough. instance if == adobe, , title adobe dreamweaver, should populate

i recommend use stripos(), or preg_match(). can use strpos() if know case of text matching going match, ie business, not business or business

if can match case sensitively can save execution time, if it's user input, should take match case insensitively.

to match case-senstively stripos():

if(strpos($crs_cat1, 'business') !== false){ //code } 

to match case-sensitvely regular expression:

if(preg_match('/business/', $crs_cat1)){ //code } 

or, match case-insensitvely (recommended usually):

if(stripos($crs_cat1, 'business') !== false){ //code } 

or, match case-insensitvely regex:

if(preg_match('/business/i', $crs_cat1)){ //code } 

typically, if you're matching on simple string in example, should stick strpos/stripos. but, if you're matching on complex type of match, preg_match of great use.

i ran performance test betweeen stripos , preg_match confirm stripos() still faster simple word match.

stripos() 10000000 iterations:

6.687420129776s 

preg_match() 10000000 iterations:

9.2206559181213s 

rocksfrow


Popular posts from this blog