1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

whats the different from include and require?

Discussion in 'PHP' started by fengfeng, Oct 2, 2007.

  1. #1
    i know the include can return value and the require cant.is any other different between them??:)
     
    fengfeng, Oct 2, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    www.php.net/require
     
    nico_swd, Oct 2, 2007 IP
  3. amherstsowell

    amherstsowell Peon

    Messages:
    261
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing.

    vars.php
    <?php

    $color = 'green';
    $fruit = 'apple';

    ?>

    test.php
    <?php

    echo "A $color $fruit"; // A

    include 'vars.php';

    echo "A $color $fruit"; // A green apple

    ?>



    The require() function is identical to include(), except that it handles errors differently.

    If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.



    <?php
    // file and get, if you need to include a file.php?query_string.

    function pinclude($file, $type, $get = null) {
    $p = explode('/', $file);
    $file = end($p);
    $dir = '';
    $n = count($p) - 1;

    for($i = 0; $i < $n; $i++)
    $dir .= $p[$i] . '/';

    if($get !== null) {
    $tmp = $_GET; // back up
    $_GET = array();
    $get = explode('&', $get);
    $n = count($get);

    for($i = 0; $i < $n; $i++) {
    if(strpos($get[$i], '=') === false)
    $_GET[$get[$i]] = 1;
    else {
    list($name, $val) = explode('=', $get[$i], 2);
    $_GET[$name] = $val;
    }
    }
    }

    ob_start();
    chdir($dir);
    require $file;
    $out = ob_get_clean();

    if($tmp)
    $_GET = $tmp;

    return $out;
    }

    $out = pinclude('./dir/yourfile.php', 'a=b&c=d&e');
    echo $out;
    // i'm sorry but i forgot post requests...
    ?>
     
    amherstsowell, Sep 20, 2011 IP
    jevchance likes this.
  4. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #4
    FWIW, I always use require_once(). This checks to be sure the file is not already included.
     
    jevchance, Sep 21, 2011 IP
  5. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #5

    if the included/required file does not exist, require returns a fatal error while include returns a notice only.

    fatal error will stop the whole script while notices will still continue to execute the script.
     
    JohnnySchultz, Sep 22, 2011 IP