2

I can't quite figure out the meaning of this statement:

set_include_path('.'
. PATH_SEPARATOR . '../library/'
. PATH_SEPARATOR . '../application'
. PATH_SEPARATOR . get_include_path());

A quick breakdown would be appreciated.

1
  • @yes123, I doesn't have to be, the PATH_SEPARATOR is probably set as ;. Commented Feb 4, 2012 at 12:19

3 Answers 3

3

It adds the two paths to the include_path so that if you include a file "../library/filename.php". you can do it by

include('filename.php');

instead of

include('../library/filename.php');

I suppose this is a part of some framework

It basically adds the folder to the php include path

Sign up to request clarification or add additional context in comments.

Comments

1

The first thing to note here is that the constant PATH_SEPARATOR is a predefined constant which allows for a cross-platform path separator (it resolves to ':' on unix-like systems and ';' on windows).

The following code would also achieve the same result but is a bit easier to read:

<?php
$paths = array('.', '../library/', '../application', get_include_path());
set_include_path(join(PATH_SEPARATOR, $paths));

or a bit more verbose, but easy to add to:

<?php

$paths[] = '.';
$paths[] = '../library/';
$paths[] = '../application';
$paths[] = get_include_path();

set_include_path(join(PATH_SEPARATOR, $paths));

Comments

0

What does php's set_include_path function do?

It sets a possible location for the php engine to look for files.

For example:

I put this in a php file called cmp.php under /home1/machines/public_html

<?php
  print "1<br>";
  require("hello.php");
  print "<br>2<br>";

  set_include_path("/home1/machines/public_html/php");

  print "<br>3<br>";
  require("hello.php");
  print "<br>4<br>";
?>

Make a new file hello.php under /home1/machines/public_html, put this in there:

<?php
print "hello from public_html";
?>

Make a second new file called hello.php under /home1/machines/public_html/php, put this in there:

<?php
print "hello from public_html/php";
?>

Run cmp.php, and you should get this:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.