Results 1 to 4 of 4

Thread: Variables

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Variables

    I'm pretty sure someone has asked this before, but lets say I have this:

    PHP Code:
    $title "My Title";

    if (
    $action == 'view') {
      
    view();
    elseif (
    $action == 'show') {
      
    show();
    }

    function 
    view() {
      echo 
    $title;
    }

    function 
    show() {
      echo 
    $title;

    Nothing shows up?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    variables in functions are considered local to that function, they do not have global scope

    you can do this to overcome it, or use the global keyword at the top of the script to change the behaviour
    PHP Code:
    $title "My Title";

    if (
    $action == 'view') {
      
    view($title);
    elseif (
    $action == 'show') {
      
    show($title);
    }

    function 
    view($title) {
      echo 
    $title;
    }

    function 
    show($title) {
      echo 
    $title;


  3. #3
    Addicted Member Martin Wilson's Avatar
    Join Date
    Mar 2002
    Location
    :)
    Posts
    236
    Or you can make the variable global:
    PHP Code:
    $title "My Title";

    if (
    $action == 'view') {
      
    view();
    elseif (
    $action == 'show') {
      
    show();
    }

    function 
    view() {
      global 
    $title;
      echo 
    $title;
    }

    function 
    show() {
      global 
    $title;
      echo 
    $title;

    What is the answer to this question?

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks. Chris, that's how I'm currently handling it. Martin, I'll give that a try when I get the chance. Thanks alot
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width