include in string ?

Discussion in 'PHP' started by meannn, Jun 21, 2010.

  1. #1
    Hello,

    Is it possible to include something in string or with function ? If not, how to do that ?
    I have to make it in string. When I use the string, it should includes it.

    $include = include("file/admin.php");
     
    meannn, Jun 21, 2010 IP
  2. bvraghav

    bvraghav Member

    Messages:
    123
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    i am not pretty sure, what you are looking forward to do...
    but, include can be used to accomplish many things.

    in case you want to avoid re-including the file, you can use
    include_once("your-include-file.php")
    Code (markup):
    you can store the filenames in a string and include them on condition
    if ( $condition ) {
        include( $include_file_a );
    } else {
        include( $include_file_b );
    }
    Code (markup):
    or you can use the code inside the include file to return a value
    main.php
    $a = $_GET['a'];
    $mytext = include( "include.php" );
    echo $mytext;
    Code (markup):

    include.php
    return ($a%2) ? "odd" : "even";
    Code (markup):
    refer http://php.net/manual/en/function.include.php
    Example #5 include() and the return() statement
     
    bvraghav, Jun 21, 2010 IP
  3. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. But I mean, how to include file instead of include("file.php");

    is there anyway to include file with function or anything else ?
     
    meannn, Jun 21, 2010 IP
  4. bvraghav

    bvraghav Member

    Messages:
    123
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #4
    there is a similar function called require()
    http://www.php.net/manual/en/function.require.php

    also you can use include to include php files via "http"

    but i am more inquisitive about what do you want to do when you say you want to "include via function"
     
    bvraghav, Jun 21, 2010 IP