Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    include global scope

    why doesnot following code run ?

    PHP Code:
    $x = include("lang.php");

    function 
    zode(){
    $GLOBALS $x;

    //codes



  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Because you don't call the function:
    PHP Code:
    $x = include('lang.php');
    zode();

    function 
    zode()
    {
        global 
    $x;
        
    // codes

    Last edited by Inigoesdr; 03-09-2008 at 09:55 PM. Reason: Typo

  • Users who have thanked Inigoesdr for this post:

    zodehala (03-09-2008)

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    103
    Thanks
    1
    Thanked 8 Times in 8 Posts
    PHP Code:
    $x = include('lang.php');
    zode($x);

    function 
    zode()
    {
        global 
    $x;
        
    // codes

    should be:
    PHP Code:
    $x = include('lang.php');
    function 
    zode()
    {
        global 
    $x;
        
    // codes
    }  
    zode(); // you don't need $x here since its global and it should be called after after declared(better coding) 
    will save you from further errors

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    It's not necessarily better coding, unless you're planning to port the code to another language. Calling the function "before" it's declared doesn't really matter in PHP because the parser loads all functions on the first pass. In other C-type languages you are required to declare a function before it's called, though. Then again, if you were using proper coding we wouldn't be requiring global here at all(which I did initially, but changed my mind to keep from confusing an apparently novice coder; that's why I left the extra variable on accident).
    Last edited by Inigoesdr; 03-09-2008 at 10:03 PM.

  • #5
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    lang.php
    PHP Code:
    $lang[user][first] = "zodehala"
    index.php
    PHP Code:
    $x = include('lang.php');
    function 
    zode()
    {
        global 
    $x;
        echo 
    $lang[user][first] ;
    }  
    zode(); 
    output is blank page why ?

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by zodehala View Post
    output is blank page why ?
    You don't need to assign the return of include() to a variable if you want to parse the page:
    PHP Code:
    include('lang.php');
    function 
    zode()
    {
        global 
    $lang;
        echo 
    $lang['user']['first'];
    }  
    zode(); 

  • #7
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    thanx and how can i use following constant in all function like lang.php

    PHP Code:
    define('CSS' '<link href="style.css" rel="stylesheet" type="text/css">',FALSE); 

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Constants are global, so as long as it's defined before you use it(via define() or in an included file), you should be able to just:
    PHP Code:
    echo CSS

  • Users who have thanked Inigoesdr for this post:

    zodehala (03-10-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •