PHP: How to set the value of the include_path: set_include_path

Various functions in PHP have options to search for files in a fixed set of directories often known as include_path. When you include another PHP file in another PHP program, the interpreter searches for the file in the directories mentioned in the include_path and reports an error if not found. The include_path value can be overwritten by a program. But it is better to extend the current value and add more directories. The following program does that. It gets the current value and extends the new value. Note the use of the PATH_SEPARATOR, which is a variable to signify the separator between two directories.

<?php
print get_include_path()."n";
$path = "../../config";
set_include_path(get_include_path().PATH_SEPARATOR.$path);
print get_include_path()."n";
?>

The output of the program is as follows

$php value.php
.:/usr/share/pear:/usr/share/php
.:/usr/share/pear:/usr/share/php:../../config

PHP: How to get the value of the include_path: get_include_path

Various functions in PHP have options to search for files in a fixed set of directories often known as include_path. When you include another PHP file in another PHP program, the interpreter searches for the file in the directories mentioned in the include_path and reports an error if not found. The include_path value can be overwritten by a program. To get the value of the include_path

<?php
print get_include_path()."n";
?>

The output of the file is generated as follows

$php value.php
.:/usr/share/pear:/usr/share/php

PHP: How to get the default value of the include_path: get_include_path

Various functions in PHP have options to search for files in a fixed set of directories often known as include_path. When you include another PHP file in another PHP program, the interpreter searches for the file in the directories mentioned in the include_path and reports an error if not found. The include_path value can be overwritten by a program. To get the default value of the include_path

<?php
print get_include_path()."n";
?>

The output of the file is generated as follows

$php value.php
.:/usr/share/pear:/usr/share/php

PHP: Extract Outgoing URLs from a Web Page

In PHP, you can download a web page using file_get_contents or curl. Once you have downloaded a web page, you can process it.

We know that the tag structure of hyperlink is as follows

<a href="http://www.example.com">Example</a>

Keeping this in mind, we write the following program

<?php

function extractElementsFromWebPage($webPage, $tagName) {
  //Creating a DOMDocument Object.
  $dom = new DOMDocument;

  //Parsing the HTML from the web page
  if ($dom->loadHTML($webPage)) {
    // Extracting the specified elements from the web page
    @$elements = $dom->getElementsByTagName($tagName);
    return $elements;
  }
  return FALSE;
}

function downloadURL($URL) {
  $webPage = file_get_contents ($URL);
  return $webPage;
}

$webPage = downloadURL("http://www.mozilla.org/");
if ($webPage ) {
  $URLs = extractElementsFromWebPage($webPage, 'a');
  if ($URLs) {
    foreach ($URLs as $URL){
      // Extracting the URLs
      echo $URL->getAttribute('href'), "n";
    }
  }
  else {
    echo "Error in parsing the webPagen";
  }
}
else {
  echo "Error in downloading the webPagen";
}
?>

There are certain things that need to be understood:

Firstly we are using file_get_contents to download a web page. Then we use the DOMDocument class in PHP to parse the HTML page. Check the two functions

  1. downloadURL
  2. extractElementsFromWebPage

downloadURL uses file_get_contents to download the web page and extractElementsFromWebPage uses the DOMDocument class. The function loadHTML is used to parse the HTML page and getElementsByTagName to extract the specified elements. In our case, we want to extract the HTML tag element a.

On executing the program

$ php extractURLs.php 
#main
/
/about/
/community/
/projects/
/contribute/
/about/mission.html
http://www.mozilla.com/firefox/
http://www.mozilla.com/mobile/download/
...

PHP: Extract Image URLs from a Web Page

In PHP, you can download a web page using file_get_contents or curl. Once you have downloaded a web page, you can process it. We want to extract the image URLs from a web page.

We know that the tag structure of an image url is as follows

<img src="image.gif" alt="Image Description" />

Keeping this in mind, we write the following program

<?php

function extractElementsFromWebPage($webPage, $tagName) {
  //Creating a DOMDocument Object.
  $dom = new DOMDocument;

  //Parsing the HTML from the web page
  if ($dom->loadHTML($webPage)) {
    // Extracting the specified elements from the web page
    @$elements = $dom->getElementsByTagName($tagName);
    return $elements;
  }
  return FALSE;
}

function downloadURL($URL) {
  $webPage = file_get_contents ($URL);
  return $webPage;
}

$webPage = downloadURL("http://www.mozilla.org/");
if ($webPage ) {
  $imageURLURLs = extractElementsFromWebPage($webPage, 'img');
  if ($imageURLURLs) {
    foreach ($imageURLURLs as $imageURL){
      // Extracting the URLs
      echo $imageURL->getAttribute('src'), "n";
    }
  }
  else {
    echo "Error in parsing the webPagen";
  }
}
else {
  echo "Error in downloading the webPagen";
}
?>

There are certain things that need to be understood:

Firstly we are using file_get_contents to download a web page. Then we use the DOMDocument class in PHP to parse the HTML page. Check the two functions

  1. downloadURL
  2. extractElementsFromWebPage

downloadURL uses file_get_contents to download the web page and extractElementsFromWebPage uses the DOMDocument class. The function loadHTML is used to parse the HTML page and getElementsByTagName to extract the specified elements. In our case, we want to extract the HTML tag element img.

On executing the program

$ php extractImageURLs.php
/images/promos/join_promo_a.png
/images/template/screen/logo_footer.png
https://statse.webtrendslive.com/dcsis0ifv10000gg3ag82u4rf_7b1e/njs.gif?dcsuri=/nojavascript&WT.js=No&WT.tv=8.6.2

PHP: Extract HTML Tags/Element from a Web Page

In PHP, you can download a web page using file_get_contents or curl. Once you have downloaded a web page, you can process it. Take for example, we want to extract the image URLs from a web page.

We know that the tag structure of an image url is as follows

<img src="image.gif" alt="Image Description" />

Keeping this in mind, we write the following program

<?php

function extractElementsFromWebPage($webPage, $tagName) {
  //Creating a DOMDocument Object.
  $dom = new DOMDocument;

  //Parsing the HTML from the web page
  if ($dom->loadHTML($webPage)) {
    // Extracting the specified elements from the web page
    @$elements = $dom->getElementsByTagName($tagName);
    return $elements;
  }
  return FALSE;
}

function downloadURL($URL) {
  $webPage = file_get_contents ($URL);
  return $webPage;
}

$webPage = downloadURL("http://www.mozilla.org/");
if ($webPage ) {
  $imageURLURLs = extractElementsFromWebPage($webPage, 'img');
  if ($imageURLURLs) {
    foreach ($imageURLURLs as $imageURL){
      // Extracting the URLs
      echo $imageURL->getAttribute('src'), "n";
    }
  }
  else {
    echo "Error in parsing the webPagen";
  }
}
else {
  echo "Error in downloading the webPagen";
}
?>

There are certain things that need to be understood:

Firstly we are using file_get_contents to download a web page. Then we use the DOMDocument class in PHP to parse the HTML page. Check the two functions

  1. downloadURL
  2. extractElementsFromWebPage

downloadURL uses file_get_contents to download the web page and extractElementsFromWebPage uses the DOMDocument class. The function loadHTML is used to parse the HTML page and getElementsByTagName to extract the specified elements. In our case, we want to extract the HTML tag element img.

On executing the program

$ php extractElements.php
/images/promos/join_promo_a.png
/images/template/screen/logo_footer.png
https://statse.webtrendslive.com/dcsis0ifv10000gg3ag82u4rf_7b1e/njs.gif?dcsuri=/nojavascript&WT.js=No&WT.tv=8.6.2

PHP: Download Web Page using file_get_contents

You can use curl to download a webpage in PHP. It is also possible to download a web page using file_get_contents().

<?php

function downloadURL($URL) {
  $webpage = file_get_contents ($URL);
  return $webpage;
}

$webpage = downloadURL("http://www.mozilla.org/");
if ($webpage){
  echo $webpage;
}
else {
  echo "Error in downloading the webpagen";
}
?>

$ php download.php
<html>
....
</body>
</html>

In the above example, we try to download the web page of Mozilla. Let’s try to download a non existing web page

<?php

function downloadURL($URL) {
  $webpage = file_get_contents ($URL);
  return $webpage;
}

$webpage = downloadURL("http://www.mozilla.org/1");
if ($webpage){
  echo $webpage;
}
else {
  echo "Error in downloading the webpagen";
}
?>

We find the following error

$ php download.php

Warning: file_get_contents(http://www.mozilla.org/1): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 in /home/user/Documents/Dropbox/Personal/Programs/downloadWebpage.php on line 4
Error in downloading the webpage

PHP: Associative Arrays

Arrays in PHP are different from other programming languages like C/C++? Java that they allow heterogenous elements and support negative index. Associative arrays are like the hashmaps or the key value pairs. In case of associate arrays, the indices can be strings. They are called associative arrays because you associate a string to another value which can be of any type.

Let’s see a simple program. We associate different room numbers to the respective courses

<?php

$room['Room 101'] = "Physics";
$room['Room 102'] = "Chemistry";
$room['Room 103'] = "Biology";
$room['Room 104'] = "Computers";
$room['Room 105'] = "Languages";

foreach ($room as $key => $value) {
    echo "The $value course will be conducted in $keyn";
}
?>

Also notice the use of foreach which converts the associate array to key-value pairs
The output

The Physics course will be conducted in Room 101
The Chemistry course will be conducted in Room 102
The Biology course will be conducted in Room 103
The Computers course will be conducted in Room 104
The Languages course will be conducted in Room 105

You can consider the arrays with integer indices as key-value pairs

$days = array("Sunday", "Monday", "Tuesday", "Wednesday",
 "Thursday", "Friday", "Saturday");

foreach ($days as $key => $value) {
    echo "Day ", $key+1,"  : $valuen";
}

The output

Day 1  : Sunday
Day 2  : Monday
Day 3  : Tuesday
Day 4  : Wednesday
Day 5  : Thursday
Day 6  : Friday
Day 7  : Saturday

PHP functions: Pass by Reference versus Pass By Value

To illustrate the difference between Pass by Reference and Pass by Value in a PHP function, we will use a simple program. Notice the two functions in the following programs changeToUpperCase() and updateArrayToUpperCase(). On closer look, you will find that the function definitions of both are same.

<?php

function changeToUpperCase($stringArray) {
    for($i=0 ; $i < sizeof($stringArray); $i++) {
        $stringArray[$i] = strtoupper($stringArray[$i]);
    }
}

function updateArrayToUpperCase(&$stringArray) {
    for($i=0 ; $i < sizeof($stringArray); $i++) {
        $stringArray[$i] = strtoupper($stringArray[$i]);
    }
}

$days = array("Sunday", "Monday", "Tuesday", "Wednesday",
 "Thursday", "Friday", "Saturday");

echo "Before calling any functionsn";
var_dump($days);

changeToUpperCase($days);
echo "After calling changeToUpperCase()n";
var_dump($days);

updateArrayToUpperCase($days);
echo "After calling updateArrayToUpperCase()n";
var_dump($days);


?>

The output

Before calling any functions
array(7) {
  [0]=>
  string(6) "Sunday"
  [1]=>
  string(6) "Monday"
  [2]=>
  string(7) "Tuesday"
  [3]=>
  string(9) "Wednesday"
  [4]=>
  string(8) "Thursday"
  [5]=>
  string(6) "Friday"
  [6]=>
  string(8) "Saturday"
}
After calling changeToUpperCase()
array(7) {
  [0]=>
  string(6) "Sunday"
  [1]=>
  string(6) "Monday"
  [2]=>
  string(7) "Tuesday"
  [3]=>
  string(9) "Wednesday"
  [4]=>
  string(8) "Thursday"
  [5]=>
  string(6) "Friday"
  [6]=>
  string(8) "Saturday"
}
After calling updateArrayToUpperCase()
array(7) {
  [0]=>
  string(6) "SUNDAY"
  [1]=>
  string(6) "MONDAY"
  [2]=>
  string(7) "TUESDAY"
  [3]=>
  string(9) "WEDNESDAY"
  [4]=>
  string(8) "THURSDAY"
  [5]=>
  string(6) "FRIDAY"
  [6]=>
  string(8) "SATURDAY"
}

As mentioned earlier, the function definitions of both are same. Even the parameters used to call the function are the same.

changeToUpperCase($days);
updateArrayToUpperCase($days);

What makes the difference is the way by which the function arguments are written.

function changeToUpperCase($stringArray) 
function updateArrayToUpperCase(&$stringArray)

If you notice, in the second case, there is an extra parameter & with the function argument. When & is used, it signifies Pass by Reference. It means that the original parameter must not be copied to a new variable. The parameter is referred inside the function by the reference. It means that any change made in the function to a parameter passed by reference, the change can be reflected in the caller function. Hence the difference in the ouptut.

PHP: User defined Functions

There are many functions provided by PHP like array(). But the users can define functions of their own. A function in PHP has the following form

function functionName(functionArguments) {
   // Body of the function
   // optional return value
}

Take for example, we want to create a function to convert an array of strings to upper case using the function strtoupper() provided by the PHP.

function changeToUpperCase($stringArray) {
    for($i=0 ; $i < sizeof($stringArray); $i++) {
        $stringArray[$i] = strtoupper($stringArray[$i]);
    }
    var_dump($stringArray);
}
$days = array("Sunday", "Monday", "Tuesday", "Wednesday",
 "Thursday", "Friday", "Saturday");

changeToUpperCase($days);

Here we can see the function changeToUpperCase being defined. It takes only one argument which is an array (of strings), changes the case of the array elements and finally prints the array using another PHP function var_dump().

The output

array(7) {
  [0]=>
  string(6) "SUNDAY"
  [1]=>
  string(6) "MONDAY"
  [2]=>
  string(7) "TUESDAY"
  [3]=>
  string(9) "WEDNESDAY"
  [4]=>
  string(8) "THURSDAY"
  [5]=>
  string(6) "FRIDAY"
  [6]=>
  string(8) "SATURDAY"
}

The advantage of using functions is to make the program modular and readable and to avoid rewriting code.