SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: php variable

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php variable

    Hi

    I have a variable $fee in file A.php, for example: $fee = $f*2; $f=100;

    In the file B.php, I have 'require_once (../A.php);'

    In the file C.php, I have 'include (../B.php);'

    In the file C.php, I want to show as: The total fee is $fee,
    which returns 'The total fee is 200'.

    How can I use the $fee in the C.php file that the result will be returned? Thanks a lot.

  2. #2
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo $fee

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $fee = $f*2; $f=100; in the A.php

    There's no variable $fee in the C.php file.

    I also use echo $fee in the C.php but returns nothing.

  4. #4
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are including something that defines $fee, $fee should be defined.

    Are you sure you have the paths right?

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya i am sure, because others work fine

  6. #6
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's kinda tricky, but when you include a file that's including another file (and you are using relative paths) then the path in all includes is relative to the original executing script (in your example, it would be 'C.php').

    To put it another way, let's say that C.php is located in /home/mysite/www/www/somedir1/somedir2/

    B.php is located in /home/mysite/www/www/somedir1/

    and A.php is located in /home/mysite/www/www/

    when C.php calls B.php via include('../B.php'), you are saying "include the file B.php which is one directory above C.php (since C.php is the original script being called.)

    The problem arises in B.php because you use a relative path, so by using include('../A.php'), you are still saying "include the file A.php which is one directory above C.php". Which is not where this file is located.

    The simplest fix is to use absolute paths, not relative ones in your includes.

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's nothing wrong with the include because it works properly with other things.

    Finally, I found out. The $fee variable is in a function in A.php so that I can't use $fee directly in C.php.

    Thanks for all your help!


Bookmarks

Posting Permissions

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