#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    12
    Rep Power
    0
    *I posted this is the Beginners Forum
    by accident. It was not meant as a cross or spam post*

    Hey guys,
    I just started to learn PHP, and I have a strong background Borland Delphi and HTML. But, I am new to this so i was hoping someone could help me out. Are their procedures? Like you have...
    function what($var) {
    blah
    }

    and in perl you can do a simple procedure without variables like

    sub whatever{
    blah
    }

    What would the 'sub' be equivilent to in PHP? I have a error message I want posted throughout a page if there are form errors, and i know i can just do include but I would rather use a procedure, at least to learn how its done. Here's the code im using...maybe i just messed up the syntax or something...

    sub ErrorMsg {
    echo "error message here";
    }

    Hope someone can help me out,
    -Bryan

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    45
    Rep Power
    15
    u declare functions as follows:

    function something()
    {
    echo "blah";
    }

    OR

    function something($var)
    {
    echo $var;
    }

    and you call them as follows

    something();
    something($var);

    and that will echo "blah" then $var. Get it?
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    12
    Rep Power
    0
    Thanks. I have one more question and I should be all set. How do i used variables that have been set outside a function? At the beginning of my script theres an area to set variables such as font characteristics and stuff, but I can't seem to use them in a function unless I have the user re-define them in another area. So is it possible to do this?

    Thanks in advance,
    -Bryan
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    45
    Rep Power
    15
    Yeah, if you want to use a variable that has been defined outside of your function in your function, there are 2 ways to do it. First is to pass it as a parameter (as shown above) and the second is to declare it as a global variable, as follows:

    function blah()
    {
    global $a;
    echo $a;
    }

    $a = 5;
    blah();

    If you did not say "global $a" then the output of that function would be nothing.

Similar Threads

  1. quick mysql/php question
    By mirthika in forum PHP Development
    Replies: 2
    Last Post: February 16th, 2004, 02:15 PM
  2. Quick regex wildcard question.
    By s200 in forum PHP Development
    Replies: 2
    Last Post: February 12th, 2004, 09:09 AM
  3. Replies: 8
    Last Post: February 8th, 2004, 12:04 AM
  4. Time Question
    By MrT in forum Perl Programming
    Replies: 1
    Last Post: February 3rd, 2004, 06:03 AM
  5. Quick GD Question
    By dirtbox in forum PHP Development
    Replies: 2
    Last Post: February 2nd, 2004, 07:24 AM

IMN logo majestic logo threadwatch logo seochat tools logo