Newbies forum FAQ + Bookmarked threads - Page 2
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 38

Thread: Newbies forum FAQ + Bookmarked threads

  1. #16
    code | beer > sleep
    Join Date
    Sep 2002
    Location
    aus
    Posts
    4,853
    IF/ELSELIF/ELSE statements

    http://www.php.net/manual/en/control...res.elseif.php See what the manual says

    Most people when starting with PHP, try to have lots of unnessacary code, which to them may look great but when needing to be fixed its a nightmare.
    IF/ELSEIF/ELSE statements are used quite reguarly when being a newbie, and in some cases are used too much.

    Lets take a look at a simple IF/ELSEIF/ELSE statement (From the Manual)

    PHP Code:
    if($a == a){
    print 
    "yes";
    }elseif(
    $a == b){
    print 
    "no";
    }else{
    // if $a != a AND $a != b
    print "who cares";

    Although the a needs to be "a" and b needs to be "b" this is what a IF/ELSEIF/ELSE statement should look like. Learning when to use this type of control structure is vital.
    Its not hard to use, but you seem to get IF/ELSEIF/ELSE structures looking something like this

    PHP Code:
    if($page "1"){
    //do something
    }elseif($page == "2"){
    //do something
    }elseif($page == "3"){
    //do something
    }elseif($page == "4"){
    //do something
    }elseif($page == "5"){
    //do something
    }else{
    //show index

    This is syntacially correct but if your goal is to become a very good programmer writing efficient code this is very much wrong.

    Why?
    IF/ELSEIF/ELSE structures should be only checking 3 things

    $a = b;
    Lets check if $a = a, if not then check if c; if not show and error

    PHP Code:
    $a “b”;
    if(
    $a == “a”){
      
    //do something
    }elseif($a == “c”){
      
    //do something
    }else{
      echo 
    “$a isn’t a or c”;

    There you go very simple. The basics of a good IF/ELSEIF/ELSE structure should be checking for 1 result if failed check for a 2nd result if both failed show an error. If your thinking of writing an Error Class or an Error Function you should consider using the IF/ELSEIF/ELSE structure.

    When don’t I use the IF/ELSEIF/ELSE structure and use the SWITCH structure?

    When there can be more than 2 options + an error statement so 3options +.
    A good example is your querystring

    e.g.

    PHP Code:
    switch($page){
    case 
    ‘1’;
    //do something
    break;
    case 
    ‘2’;
    //do something
    break;
    case 
    ‘3’;
    //do something
    break;
    case 
    ‘4’;
    //do something
    break;
    default:
    //show other
    break;

    This is much better than viewing this

    PHP Code:
    if($page "1"){
    //do something
    }elseif($page == "2"){
    //do something
    }elseif($page == "3"){
    //do something
    }elseif($page == "4"){
    //do something
    }else{
    //show other

    Not only is it better looking but its much faster instead of checking every if/elseif/else to see when something doesn’t fail. The Switch structure just checks if 1 do this 2 do this and so on. There probably wouldn’t be a great difference in time using just that example, but what happens if you use this on a page many times. There could be 1 to even 2seconds loading difference, and in this day and age that’s a long time.
    Dont be lazy Search
    Use the Manual

  2. #17
    Junior Member
    Join Date
    Oct 2003
    Location
    seattle
    Posts
    1

    Many Thanks

    Just wanted to say thanks for taking the time to spell things out so simple. It helps a lot.

  3. #18
    code | beer > sleep
    Join Date
    Sep 2002
    Location
    aus
    Posts
    4,853
    I thought it was about time i added to this.


    To OOP or not to OOP.

    The Manual your best guide - http://au.php.net/manual/en/language.oop.php

    With PHP5 coming out in the very soon future i expect more and more programmers (PHP Programmers) to use OOP. There are many users doing so now but PHP5 has much more to offer to OOP.


    So what is OOP?
    OOP or Object Orientated Programming is basically a collection/group of functions (a Class) to achieve goal (one goal that is).


    Functions VS Classes
    The fight isnt over yet.

    Most new to programming tend to think why do i need a class? i can do the same with functions and procedural programming.

    So what advantages does Classes (OOP) have over Functions. Well firstly being a lazy programmer so to speak, i dont want to create something i have done before. A Database class for example, i use Databases in almost every project i do (i suspect the same from most others)

    So instead of doing it over and over again a class can organise functions (Objects) in this case the ability to use other Databases besides MySQL.

    However in the aproach of functions you'd need to create each function and if you wanted to use another database, you would have to create a function something like this

    PHP Code:

    //connection to database

    function mysqlConn() {
      
    mysql_connect();
    }

    function 
    mSQLConn() {
      
    msql_connect();
    }

    function 
    oracleConn() {
      
    ora_plogon();

    Then from there you will need to check which database type will be used. Adding many lines to code. Of course you can just create a new file and just include it all the time but you will still need to do something similar to this can isnt very organised.

    Example - Database Class

    As i have been explaing things about databases and OOP. I thought a database class for MySQL will go good

    PHP Code:

    class DB {

    #Define Class Variables

    var $host;
    var 
    $user;
    var 
    $pass;
    var 
    $db
    var 
    $linkid;

      function 
    conn() {
        
    $this->linkid mysql_connect($this->host,$this->user,$this->pass);
          
         if (!
    $this->linkid) {
           
    $this->error(mysql_error());
         } else {
        if (!
    mysql_select_db($this->db,$this->linkid)) {
          
    $this->error(mysql_error());
        }
         }

      }

      function 
    query($sql) {
        
    $query mysql_query($sql);
        if (!
    $query) {
          
    $this->error(mysql_error());
        } else {
          return 
    $query;
        }
      }

      function 
    fetcharray($array) {
        
    $fetch mysql_fetch_array($array);
        if (
    is_array($fetch) && count($fetch) != 0) { 
          return 
    $fetch;
        } else {
          return 
    false//more than likely there are no rows or an error occured
        
    }
      }

      function 
    rows($rows) {
        
    $num mysql_num_rows($rows);
        if (
    $num != 0) {
          return 
    $num;
        } else {
          return 
    false//no data to be sent
        
    }
      }

      function 
    error($err) {
        
        @
    mail('myemail@mail.com','Database Error','An error occured in database: '.$err,'FROM: Database Class'); //notify admin
        
        
    $msg 'Database Error<br />An error has occured in the database, an email has been sent to notify an Admin';
        die (
    $msg);

      }


    Although it doesnt look pretty for an example and may not work (Sorry i didnt test it) you can add to it can reuse it in all your projects.



    Explanation
    So what does some of the code mean

    var $host;
    This is basically a variable that is defined only for the class. However you can do this to change its value.
    PHP Code:
    $dbclass = new DB//creates new DB class
    $dbclass->host 'blah'
    When used in the class and returned
    PHP Code:

    echo $this->host;

    #OUTPUT: blah 
    $this->

    You saw before outside the class you must create it with a variable and acces s it like $dbclass->host

    $this-> is basically a global variable for use only in a class, meaning you cannot use $this->host; outside the class as it will output and error such as undefined class variable.

    My example probably isnt best a contructor isnt used (Gives slap on wrist). Also read the link i gave, there a few things that cannot be used in a class as are used by Zend or are 'Magical' in PHP class.

    Hope this helps for starters hope to add more to this with extends, constructors, and how to use PHP4 classes in PHP5 but well wait for PHP5 to be stable.
    Dont be lazy Search
    Use the Manual

  4. #19
    code | beer > sleep
    Join Date
    Sep 2002
    Location
    aus
    Posts
    4,853
    There were links previously in this document on how to install PHP/Apache/MySQL but we were installing PHP as a CGI.

    I'm sure most users would like to use PEAR and extensions in PHP.

    Sorry this tutorial is for PHP users on Windows XP only it may work for other Win2k systems but it may be varied I suggest if you are not on a Windows XP system to read here

    http://www.php.net/manual/en/install.windows.php

    Installing PHP as a Module

    Installing Apache 2.0.48

    Lets download Apache first http://httpd.apache.org/ when I was installing I used Apache 2.0.48 but it will only have difference when we go to add the PHP to Apache.

    Installing Apache is quite easy its pretty much click Next, Typical or Custom I suggest clicking typical setup. If you don’t have a server of any kind whether it be a downgraded version of apache, IIS or something different, please make sure you install it on Port 80 HTTP. It will say something along install for all users make sure that is checked. Also for where to install to you can modify that of course I did it in c:\apache as I already have an installation of Apache 1.3.x on my machine

    Okay to check Apache is installed http://localhost if you see a screen with the apache logo and test confirming it has been installed give yourself a pat on the back.

    Downloading and Installing PHP 4.3.4

    http://au.php.net/get/php-4.3.4-Win32.zip/from/a/mirror select the mirror and download into the apache folder. Extract the files into the apache folder. It should create a folder called php4.3.4-Win32 or something similar, change that to PHP.

    Find this file in the php folder php.ini-recommended
    Rename it so its php.ini

    Open the file php.ini and find this
    extension_dir

    Make it this

    extension_dir = "c:\apache\php\extensions" change it to co-inside to where the php extensions folder is.

    Once done save this file in your c:\apache\apache2\bin folder.

    Find php4ts.dll in your php folder copy and paste it into your c:\windows\system32 if you’re on XP or find your system root %SYSTEMROOT% c:\WINNT is NT/2000 Users

    That’s all you need changed.

    Creating a batch file to Start/Restart/Stop Apache

    In the Apache Directory not Apache2. We will create 3 files in notepad


    Code:
    @echo off
    echo Start Apache 2.0 Server please Wait.
    cd c:\apache\apache2\bin
    
    apache -k start
    
    echo Apache successfully started
    pause
    Save as apachestart.bat

    Code:
    @echo off
    
    echo Ending Apache 2.0 Service
    
    cd c:\apache\apache2\bin
    
    apache -k stop
    
    echo Apache 2.0 Service Stopped
    
    pause
    Save as apachestop.bat

    Code:
    @echo off
    echo Restarting Apache 2.0 Server please Wait.
    cd c:\apache\apache2\bin
    
    apache -k restart
    
    echo Apache successfully Restarted
    pause
    Save as restartapache.bat

    Now let’s bind PHP and Apache

    In the Apache/Conf folder open httpd.conf, and add these lines

    LoadModule php4_module "c:/apache/php/sapi/php4apache2.dll" //change this to suit your PHP installation directory
    AddType application/x-httpd-php-source .phps
    AddType application/x-httpd-php .php

    Save the file and restart Apache by using the restartapache.bat file.

    Now let’s see if we installed PHP correctly. In the htdocs folder in Apache2 Folder create a file called phpinfo.php

    PHP Code:
    <?php

    phpinfo
    ();

    ?>
    Save the file and go to http://localhost/phpinfo.php if you see the PHP Information page congratulations you just installed PHP as a Module if you didn’t and only see source code, please go over this tutorial to make sure you didn’t miss anything also check the PHP Link as well to verify you did it correct for your Operating System.

    In my 2nd Step to this Tutorial we will install Pear and some of the popular extensions.

    --This Tutorial may need to be updated if it is not correct or missing sections. Please PM if you have trouble following this tutorial.
    Dont be lazy Search
    Use the Manual

  5. #20
    Senior Member DXL's Avatar
    Join Date
    Jan 2004
    Location
    Baltimore, MD
    Posts
    74

    Question

    umm...planetism.
    im the newest member to this site, and i had already tried installing php with a apache server, and when i did that localhost thing, with index.php, it came up as a zip file for a home publishing project.
    so then, i removed it all like this guy said(in a yahoo! chat room), and got easyphp, like he said...what should i do now?
    *-_~ALL WHO STAND IN MY WAY SHALL PERISH BY MY HAND*-_~

  6. #21
    code | beer > sleep
    Join Date
    Sep 2002
    Location
    aus
    Posts
    4,853
    I dont like distros like that most are quite limited, although i havent run EasyPHP.

    Did you save the files in the document_root?

    Also i should have mentioned 127.0.0.1 is the same as localhost, so if localhost doesnt work (it should anyway) you can try 127.0.0.1

    Also what version Apache and PHP did you download?
    Dont be lazy Search
    Use the Manual

  7. #22
    Senior Member DXL's Avatar
    Join Date
    Jan 2004
    Location
    Baltimore, MD
    Posts
    74

    Post

    no, i didn't know how to work easyphp, since the instrustions were in spanish.

    i didn't know that localhost could be entered a different way.

    i uninstalled easyphp, then did the apache thing over, with apache 2, and i have php 4.3.whatever

    ive tried this about 6 times, and failed each time

    my Os is Windows 98

    and, as a final note, im having the hardest time of my life over here, trying to install it, and make it run properly
    *-_~ALL WHO STAND IN MY WAY SHALL PERISH BY MY HAND*-_~

  8. #23
    code | beer > sleep
    Join Date
    Sep 2002
    Location
    aus
    Posts
    4,853
    Okay sorry i should have asked what OS you are running

    Sorry but ill refer you here basically i tone it down to easy understanding what

    http://www.php.net/manual/en/install.windows.php

    Okay firstly Windows 98 if i remember correctly not sure if its changed but Apache 2.0 doesnt work on it.

    So you'll need to downgrade.

    Also this

    LoadModule php4_module "c:/apache/php/sapi/php4apache2.dll" //change this to suit your PHP installation directory
    AddType application/x-httpd-php-source .phps
    AddType application/x-httpd-php .php

    Will need to change

    to this

    LoadModule php4_module "c:/apache/php/sapi/php4apache.dll" //change this to suit your PHP installation directory
    AddType application/x-httpd-php-source .phps
    AddType application/x-httpd-php .php

    Also make sure you have the path to php correct

    Before i go on are you recieving any error when apache starts? does apache start if so do you see the Apache Congradulation page?

    Instead of saving php4ts.dll in c:\windows\system32 it will need to be
    C:\Windows\System

    If the Batch Files (*.bat files) do not work it will more than likely be the path to Apache\Bin folder just verify that its correct.

    Im sorry that it hasnt worked for you. If it still doesnt work just try your best to tell me what you did and any errors if any your getting, this way i can help you better (PM me if you rather done that way)
    Last edited by planetsim; 01-30-2004 at 11:29 PM.
    Dont be lazy Search
    Use the Manual

  9. #24
    Senior Member DXL's Avatar
    Join Date
    Jan 2004
    Location
    Baltimore, MD
    Posts
    74
    if u have a yahoo account, get messenger, and add hydragundam7 to your list, and talk to me.
    if u dont, plz try to get one, but otherwise u can email me at hydragundam7@yahoo.com
    the reason im saying this, is i dont want to disrupt your guide with my n00bish qualities to PHP.
    *-_~ALL WHO STAND IN MY WAY SHALL PERISH BY MY HAND*-_~

  10. #25
    code | beer > sleep
    Join Date
    Sep 2002
    Location
    aus
    Posts
    4,853
    Anyone having trouble doing this please note it only works for 2000, XP user DXL couldnt get it to work on Win98, i think it maybe because of the version installed etc.

    Ill post a Win98 installation when i can grab hold of one an install it successfully.
    Dont be lazy Search
    Use the Manual

  11. #26
    Junior Member
    Join Date
    May 2004
    Location
    WV
    Posts
    4

    APACHE/PHP configured, Whats next

    THanks for your guidance Planetsim. You sure were a big help. Being the newbiest member here and having the least amount of php knowledge (i think) I can truly say it helps to have it laid out in plain words. I now have apache and php rolling per your instructions as a local test server with the exception of apache manually started with server root of localhost:8080? Did I say that right?

    I have a php-nuke theme template purchased and ready to upload but I need to determine where and how to install PHP-Nuke. My host (avahost.net) has php, mysql and apache installed but said I will need to install the php-nuke. I have no idea where this goes. I believe it goes in htdocs on a local server but no idea where it goes on production server.

    My web directory at avahost looks similar to this

    www/cgi-bin, chat, entropy banner, scgi-bin, wvsf

    the wvsf folder houses my static pages for my current website(www.wvsf.net)

    Where does PHP-Nuke go on my local server and if you can tell, my production server?. Any help is greatly appreciated!

    p.s. I have already installed MySQL too.
    Last edited by CADDMAN; 05-13-2004 at 04:50 PM.

  12. #27
    code | beer > sleep
    Join Date
    Sep 2002
    Location
    aus
    Posts
    4,853
    Its good to know the tutorial worked

    As for your PHPNuke, im not 100% sure on how the production server is setup however it should be possible to install where you have your static files (HTML) Id just make a folder if you dont want to go over the files or keep it seperated from the static ones.

    wvsf - Probably where you should install it, if not there try your CGI-BIN folder, but thats mainly for CGI things Perl etc.
    Dont be lazy Search
    Use the Manual

  13. #28
    Junior Member
    Join Date
    Jul 2004
    Location
    Costa Rica
    Posts
    1

    mail trouble

    Hi there, i am having problems sending an email. Here is the code, and i have found out that the "\n" is the trouble maker. Somehow it doesn`t work. This code actually sends the message but the message body doesn't appear.

    <?php
    $info="Information submitted: \n\n";
    $info+=$lastname." ".$name.";

    if(mail($email."@".$at,"Confirmacion",$info))
    {
    $Titulo="Revision de Datos";
    }
    else
    {
    $Titulo="Intengo fallido de envio de datos!!";
    }

    ?>

  14. #29
    code | beer > sleep
    Join Date
    Sep 2002
    Location
    aus
    Posts
    4,853

    Functions

    Functions

    Functions are extremely useful things in programming, PHP isnt the only language with functions, so users from other languages such as C should feel right at home when using User Defined PHP Functions.

    So What is a Function?
    A Function is a small piece of organised code programmed to do a certain task. So why not just use the standard procedural approach?

    Functions are a great thing they can be used more than once. So say you were creating a script that displays only php files in all directories from a certain level this would firstly require a script to firstly find the starting level, then find all directories. Then to read all directories. This is quite big script already, and say we than later on want to get only .html files as well. We would need to do this all over again.

    PHP Code:
    <?php

    //small skeleton example, not tested and the logic is probably not exact this is more of an overview
    $dir opendir("/my/path");
    $files = array();

    while ((
    $file readdir($dir)) !== false)
    {
      if (
    is_dir("/my/path/".$file))
      {
        
    $dirs[] = "/my/path/".$file;
        
    // loop again
      
    }
      else
      {
        
    $path_parts pathinfo("/my/path/".$file);
        if  (
    path_parts['extension'] == "php")
          
    $files[] = "/my/path/".$file;
      }
    }
    This technique in this situation is rather tedious and also very inefficient, a function is a much better alternative.

    PHP Code:
    <?php

    function getExFiles($path$extension)
    {
        static 
    $files = array();
        if (
    is_dir($path))
        {
            if (
    substr($path, -1) == "/")
                
    $path substr($path0, -1); //get rid of /
            
    $dh opendir($path);
            while ((
    $file readdir($dh)) !== false)
            {
                if (
    $file != '.' && $file != '..')
                {
                    if (
    is_dir($path."/".$file))
                    {
                        
    getExFiles($path."/".$file$extension);
                    }
                    else
                    {
                        
    $pathInfo pathinfo($path."/".$file);
                        if (isset(
    $pathInfo['extension']) && $pathInfo['extension'] == $extension)
                            
    $files[] = $path."/".$file;
                    }
                }
            }
            return 
    $files;
        }
        else
            return 
    false//no point in continuing its not a directory bad call somewhere
    }

    print_r(getExFiles('/home/user/public_html','php'));

    ?>
    As you can see thats the basis of the function. Quite small, now see as its looping through the directory than finds another directory and uses the function within the function. This is called recursion, basically looking at the script we are calling the same function within the function. Basically doing another entire loop itself.

    Also we can use this more than once so instead of having to rewrite this function we can use it again and the benefit is that we can also change the extension

    PHP Code:
    <?php

    function getExFiles($path$extension)
    {
        static 
    $files = array();
        if (
    is_dir($path))
        {
            if (
    substr($path, -1) == "/")
                
    $path substr($path0, -1); //get rid of /
            
    $dh opendir($path);
            while ((
    $file readdir($dh)) !== false)
            {
                if (
    $file != '.' && $file != '..')
                {
                    if (
    is_dir($path."/".$file))
                    {
                        
    getExFiles($path."/".$file$extension);
                    }
                    else
                    {
                        
    $pathInfo pathinfo($path."/".$file);
                        if (isset(
    $pathInfo['extension']) && $pathInfo['extension'] == $extension)
                            
    $files[] = $path."/".$file;
                    }
                }
            }
            return 
    $files;
        }
        else
            return 
    false//no point in continuing its not a directory bad call somewhere
    }

    print_r(getExFiles('/home/userl/public_html','html'));

    ?>
    Hopefully you can see the advantage of functions. If not in time you will, this example is beyond most new users to PHP, however those coming from other languages should be able to see the logic. Unfortunately for those that dont copy it down and create a file and test. See what happens, play around with it.

    How to use a Function
    Most new users still use HTML inside there PHP File. Now personally I have nothing agaisnt them its all part of the learning curve however I tend to have the HTML and PHP seperate, this isnt part of the tutorial so I wont be showing a way to do this.

    The basic idea of a function is really so it can be reused. If you only need to use something once the function is pointless and procedural programming is probably a better alternative, the other idea behind function is organisation. Organising code to be able to use them is a good habit to get into especially if you create useful functions which can be used in other projects.

    Now since functions are there to be used more than once the function must return something to the script that called for the function.

    PHP Code:
    <?php
    function max($a,$b)
    {
      if (
    $a $b)
        return 
    $a;
      else
        return 
    $b;
    }
    ?>
    This is similar to that of the actual PHP Max function so dont use it as its already defined. But as you can see a variable is return either $a or $b;

    Another way most new users to functions do is something like this

    PHP Code:
    <?php
    function max($a,$b)
    {
      if (
    $a $b)
        echo 
    $a;
      else
        echo 
    $b;
    }
    ?>
    Now there isnt anything really wrong with this, however what happens if we want a variable to be assigned to the max number, as you hopefully know echo is an output function so we cant get the number as its gone to output.

    This is where return comes into the picture. A return doesnt really need to return anything useful we could return a boolean (true OR false). And a variable can be assigned to function and get return value. This is why the first max function is much better than the second.

    With echo a lot of new users with using functions tend to want to have HTML put in the function, again there isnt anything wrong with this it will work however it tends to throw out the reason for functions which is to be able to reuse them.

    What is an Argument
    No not that type of argument when someone yells. An argument is what is assigned in the function

    PHP Code:
    <?php
    function func($arg1$arg2);
    ?>
    $arg1 and $arg2 are arguments for the function. However there is something else we need to learn parameters, lets go back to the max function

    PHP Code:
    <?php
    function max($a,$b)
    {
      if (
    $a $b)
        return 
    $a;
      else
        return 
    $b;
    }
    $max max(1,5);
    ?>
    As we can see we assigned a variable and than assigned it to the max function. So what is 1 and 5. Well they are the function paramters.

    Defined Arguments
    A defined argument is something that is associated to an argument variable, its the default value. E.g. we can make it that we dont want to have any parameters in a function yet we have 1 argument lets take a look at what I am on about.

    PHP Code:
    <?php
    function makecoffee($type "cappuccino")
    {
       return 
    "Making a cup of $type.\n";
    }
    echo 
    makecoffee();
    echo 
    makecoffee("espresso");
    ?>
    As you can see $type is defaulted to cappuccino now the first call to the function will output

    Making a cup of cappuccino
    While the second will be

    Making a cup of espresso
    As you can see this is quite a useful task, we can make out functions more dynamic, and still make them reusable. [More Reading on Arguments]

    Globals / Static
    In our first example of a function there was a keyword used "static"

    PHP Code:
    static $files = array(); 
    So what does it do exactly?
    You will find that static and global keywords will be something often used in functions more so global. Static allows the variable value to stay in the function so when its used again the value is still present. Now this makes the first function with directories not very well prepared you see if we want to get another extension it will be joined on with the last call.

    PHP Code:
    <?php

    function sum($a$b)
    {
      static 
    $c 5;
      
    $c += $a $b;
      return 
    $c;
    }

    echo 
    sum(56); // will output 16 e.g $c = 5 + 5 + 6 = 16
    echo sum(56); // will output 27 e.g. $c = 16 + 5 + 6 = 27
    ?>
    So what about global?

    Global makes a variable that is outside a function accessible inside the function. This may be a better alternative then static in our directory example since it stay in the function, so once the function as finished the global can than retain the value.

    So why not used global?
    So global may seem like a very attractive option and it is and many PHP Programmers use it, I do. However global again just throws out the very existance of why the function was created. So it can be reused. Now if you want it only reused in the same script its alright go for it however what happens when you wish to use it in another script? Using global will basically need the function to be modified just to work.

    What happens if you are working in a group, no one will really know about the global and will get unexpected results when using your function.

    So when do i use global and static?
    Well there is always a time. Now global can be used properly within a group project however it must be noted. A global if the variable doesnt exist outside the function will be created otherwise. Static on the other hand is useful if you wish to use results from prior calls to the function. Again it must be made aware to other programmers that first use will give correct output while using the function again will return unexpected results.

    So whats the better alternative?
    Using arguments in place of static and global, but remember the function may require results like with our sum to give weird results or use variables from outside however static is safer and is reusable while global isnt.

    More reading in the Manual on functions which goes a little deeper than this tutorial PHP Manual on Functions
    Dont be lazy Search
    Use the Manual

  15. #30
    Junior Member
    Join Date
    Feb 2005
    Posts
    15
    I think another way to access a variable value rather than specify $_POST['example'] each time you want to display the value is to write the following script at the beginning of your PHP script:

    $extract[$_POST]

    By doing this, it will extract all the $_POST variable and assign it to a variable

    Then when you want to access the value, just type $name, $phone rather than $_POST['name'], $_POST['phone']......


    Please correct me if im wrong

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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