Currently Browsing: PHP Array

Exclude or include with PHP in_array

PHP in_array function is very helpful if you want to include or exclude php code of files based on a certain criteria like id’s or $_GET variables.

Let’s imagine that you have a list of numbers that we want to use to include a file.

  1. < ?php
  2. $list = array(1,5,10);
  3. $id = 1; // set $id to a value from the array
  4.  
  5. if(in_array($id, $list)) { // check if is in array with in_array(what, in what);
  6.  include('file.php');
  7. }
  8. else {
  9.  echo "The number is not in the array";
  10. }
  11. ?>

If you want the get the page id or some parameter from the url using $_GET variable use the code snippet below and display content based on the id’s value.
(more…)