spryce Posted April 26, 2011 Share Posted April 26, 2011 I have a web server which contains a public site and a private site. I want to be able to test for public or private using the page title and return the appropriate header which contains a different banner, css etc. Firstly - the syntax below seems to be incorrect because it doesnt work correctly. More importantly how do I add all the values to an array and than iterate through it to check for a matching page title? public function publicSite() { if ($this->getTitle()== 'Home' || 'About Us'||'Registration' || 'Sitemap' || 'Contact Us' || 'Useful Links' || 'Feedback') { return true; } return false; } Then later on I would have: if ($this->publicSite()) { require($ROOT.'interface/pages/publicheader.php'); } else { require($ROOT.'interface/pages/privateheader.php'); } Thanks heaps! Quote Link to comment https://forums.phpfreaks.com/topic/234744-how-to-check-an-array-for-a-matching-string-and-return-true-newbie-help/ Share on other sites More sharing options...
kney Posted April 26, 2011 Share Posted April 26, 2011 try this public function publicSite() { if ($this->getTitle()== 'Home' || $this->getTitle()== 'About Us'|| $this->getTitle()=='Registration' || $this->getTitle()== 'Sitemap' || $this->getTitle()=='Contact Us' ||$this->getTitle()== 'Useful Links' || $this->getTitle()=='Feedback') { return true; } return false; } Quote Link to comment https://forums.phpfreaks.com/topic/234744-how-to-check-an-array-for-a-matching-string-and-return-true-newbie-help/#findComment-1206326 Share on other sites More sharing options...
saurabhx Posted April 26, 2011 Share Posted April 26, 2011 kney's reply is syntactically correct, but a more appropriate way will be this:- $titles = array('Home', 'About Us', 'Registration', 'Sitemap', 'Contact Us', 'Useful Links', 'Feedback'); if (in_array($this->getTitle(), $titles)) { $this->publicSite() = true; } else { $this->publicSite() = false; } Quote Link to comment https://forums.phpfreaks.com/topic/234744-how-to-check-an-array-for-a-matching-string-and-return-true-newbie-help/#findComment-1206327 Share on other sites More sharing options...
kney Posted April 26, 2011 Share Posted April 26, 2011 kney's reply is syntactically correct, but a more appropriate way will be this:- $titles = array('Home', 'About Us', 'Registration', 'Sitemap', 'Contact Us', 'Useful Links', 'Feedback'); if (in_array($this->getTitle(), $titles)) { $this->publicSite() = true; } else { $this->publicSite() = false; } Thanks .. I didn't know how to use arrays anymore, that's why the 'crappy' reply Quote Link to comment https://forums.phpfreaks.com/topic/234744-how-to-check-an-array-for-a-matching-string-and-return-true-newbie-help/#findComment-1206328 Share on other sites More sharing options...
spryce Posted April 26, 2011 Author Share Posted April 26, 2011 Thanks to both of you! saurabhx for sowing me what I needed to do. kney for sorting my dodgy syntax. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/234744-how-to-check-an-array-for-a-matching-string-and-return-true-newbie-help/#findComment-1206333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.