Page 1 of 4 1234 LastLast
Results 1 to 25 of 93

Thread: Basics Of PHP

Hybrid View

  1. #1

    Basics Of PHP

    I figured it's time to make a small tutorial , by reading this tutorial you should have the basic know how to execute small script's on your php enabled servers. Note that i will not be explaining how to set up a web server + php.

    What Does PHP Stand For?

    PHP- Hypertext Preprocessor

    Starting & Ending Tags


    We start off by learning the 2 main tag's that you must never forget , that is the starting and ending tag's.

    Starting
    Code:
    <?php
    Ending
    Code:
    ?>
    Baisc Syntax

    Now that you know about the starting & ending tag's , let's start with a basic script.
    Code:
    <?php
    echo "This is my first php script!";
    ?>
    so you have noticed echo , echo is simple, it output's the text "This is my first php script!" , now you must end this with a semicolon , why? the semicolon is a seperator and is used to distinguish one set of instructions from another.

    Variables

    Now that you know some basic syntax let's start working on variables , variables are ton's of fun and one of the most important part's of PHP.
    Code:
    <?php
    $myfirstscript = "This is my first php script!";
    echo $myfirstscript;
    ?>
    So you have noticed $myfirstscript , i made the variable $myfirstscript that hold's the string "This is my first php script!" . Then ofcourse just echo'd the variable which would output "This is my first php script!".

    Comments

    Comments are also another important thing not only in php but all languages , there used to disable any type of information inside them from actually executing with your script which ofcourse would likely cause error's.
    Code:
    //This is a comment
    /*This is
    a
    comment block*/
    Condition Statements

    Now these are fun to play with , they are used to execute certain code on a condition.
    Code:
    <?php
    $mybrowser = "Fire Fox";
    if ($mybrowser == "Fire Fox") { echo " You are using firefox!"; }
    else { echo " You are not using firefox"; }
    ?>
    So here i have set a variable equal to my browser then made a condition, if my browser is equal to firefox, output "You are using firefox" else (if your using something else) output "You are not using firefox" . Ofcourse this will not work untill i i add more to it but it give's you all a idea how condition statements work.

    Looping


    Looping is used for many many thing's , there are more then just one loop statement, ill explain how to do the "While Loop".
    Code:
    <?php
    $z = 1;
    while ($z <= 5) {
    echo $z;
    $z++
    }
    Again i made a variable named z with the value of 1 , then stated while z is less or equal to 5
    Code:
    while ($z <= 5)
    output the value of z
    Code:
    echo $z
    , increase z by 1
    Code:
    $z++
    . Which would output 1-5 on your page. Simple isn't it?

    Forms


    Now im assuming you have some basic understanding of html, although i will give an example of the html form but i will not be explaining it.
    Code:
    <form action="form.php" method="post">
    Name: <input type="text" name="name">
    <input type="submit" value="Submit">
    That's the html form , very simple. Now to the fun part.
    Code:
    <?php
    $name = $_POST["name"];
    echo "Hello ". $name;
    ?>
    Now here you have noticed something new and that would be "$_POST["name"]", $_POST is a variable already set by php , it recieved the information givin in the html form , notice the name="name" in the html form and ["name"] in $_POST.

    Now this is the end of this small basic tutorial , you should now have the knowledge to execute simple script's on your server's.

  2. #2
    I would suggest editing $mybrowser = "Fire Fox"; to something less browser related. There are actually people out there who might think this code determines what browser is being used!

  3. #3
    Join Date
    May 2002
    Location
    NE USA (Almost Canada)
    Posts
    993
    Just An Extra Note:

    If you would like to get the PHP information from your site:

    With a plain text editor paste this in:
    Code:
    <?php phpinfo() ?>
    Name it "something.php" (or even info or phpinfo.php) and upload and then open in your browser.

    Figured that'd be good for folks who are just starting on the basics and if it's covered anywhere here already I'm not really sure.

    KGIII
    What Hosting Should Be
    A Blog as CMS, eBay, PR, and Ads?
    My personal email: KGIII <at> KGIII <dot> INFO
    Microsoft MVP 2003/2006 Categories: Shell/User and Internet Explorer (G³ Solutions)

  4. #4
    nice one, ive already learnt this but i havent used php in a while so its nice to refresh my memory abit, great post for people who want to get into php

  5. #5
    Join Date
    Apr 2005
    Posts
    1,767
    Actually for a phpinfo() I would do this, since it volunteers a lot of information about your server, that you may not neccessarily want being public.

    PHP Code:
    <?PHP

    $ip 
    getenv("REMOTE_ADDR");
    $allowed_ip "24.211.80.102"// change to your ip

    if ($ip == $allowed_ip) {

    phpinfo();

    } else {

    header("Location: /");

    }

    ?>

  6. #6
    Join Date
    Feb 2006
    Location
    Buffalo, NY
    Posts
    1,501
    Or just create the file when needed / don't name it phpinfo.php

    (Or just add a constant to the index.php to show phpinfo if defined, etc)
    Cody R.
    Hawk Host Inc. Proudly Serving websites since 2004.
    Official Let's Encrypt Sponsor

  7. #7
    Is there a function in PHP that's similar to CFHTTP? I need to read the contents of one of our sites.

  8. #8
    Is it better to use $_request instead of $_POST or $_GET? Since it does what $_POST and $_GET do anyway. Is there a drawback to using it?

  9. #9
    Join Date
    Feb 2003
    Location
    AR
    Posts
    2,382
    If your form is ONLY using the post method, use $_POST. Likewise for $_GET. If your form should accept both methods, use $_REQUEST.
    Kevin

  10. #10
    Join Date
    Jun 2006
    Location
    California
    Posts
    8
    Good short, tutorial, good for new people wanting to learn PHP.

    Next thing functions, classes and arrays!

  11. #11
    Join Date
    Mar 2006
    Posts
    984
    Nice block from SNC. However, I'd state this line:

    PHP Code:
    $ip getenv("REMOTE_ADDR"); 
    for:

    PHP Code:
    $ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : getenv("REMOTE_ADDR"); 

  12. #12
    Join Date
    Aug 2010
    Location
    Belgium
    Posts
    47
    Quote Originally Posted by horizon View Post
    Nice block from SNC. However, I'd state this line:

    PHP Code:
    $ip getenv("REMOTE_ADDR"); 
    for:

    PHP Code:
    $ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : getenv("REMOTE_ADDR"); 
    You can just do $_SERVER['REMOTE_ADDR'];
    isset and getenv will only slow it down, $_SERVER and getenv do just the same, the only difference is that you can do getenv('remote_addr') but function call is slower than array call (#micro optimization)

    Quote Originally Posted by risoknop View Post
    First of all, I suggest abiding to some coding standards and/or best practices, one line condition statements for example aren't recommended.

    [code]
    Secondly, you should use enctype attribute with HTML forms for security reasons, for instance:

    Code:
    <form enctype="application/x-www-form-urlencoded" method="post" action="/controller/action">
    ]
    This is false as well, this enctype is default one so you don't have to specify it only if you need something different. The less text in your html the smaller so faster.

  13. #13
    Join Date
    Dec 2006
    Location
    PA
    Posts
    99
    thank u so much this is nice short tutorial

  14. #14
    Nicely put togethor, well done

  15. #15
    Anyone seriously looking to learn PHP should bookmark the site below. If you have any questions they can usually be answered there.

    http://www.php.net/

    The function reference is particularly useful.

    http://www.php.net/manual/en/funcref.php

  16. #16
    Join Date
    Sep 2004
    Location
    Chennai , India
    Posts
    4,632
    Quote Originally Posted by vkess
    Anyone seriously looking to learn PHP should bookmark the site below. If you have any questions they can usually be answered there.

    http://www.php.net/

    The function reference is particularly useful.

    http://www.php.net/manual/en/funcref.php
    There is no better books than the PHP manual, but the only problem is most of people find it to read e books.

  17. #17
    Join Date
    Oct 2011
    Posts
    30
    Brilliant post!!
    really its very awesome and useful tips for me. I do not know PHP but it seems interesting so I definitely start learning in a few days.
    Thank you

  18. #18
    Major thanks for the article. Will read on...

  19. #19
    Great tutorial you have posted and some very knowledgeable replies to go with it. Even if you know PHP, it is still nice to refresh your mind once in a while to remember everything about it. Some very good points made for sure.

  20. #20
    Join Date
    Sep 2012
    Location
    Frankfurt
    Posts
    22
    Pretty nice topic for beginners.

  21. #21
    Nice, great PHP tutorial for beginners. Do you intend to create more parts of this tutorial?

  22. #22
    I would add, that you must not end PHP Scripts with
    PHP Code:
    ?> 
    , it can create errors when you come to PHP Classes

  23. #23
    This post is also great post. I love it because I am beginner in this feild.

  24. #24
    Join Date
    May 2009
    Location
    India
    Posts
    153

    Basics Of PHP

    Thanks Mr. X.
    Mobile App Development Company
    Mobile Programming for iOs, Android, Windows Phone, Blackberry

  25. #25
    Quote Originally Posted by vkess View Post
    Anyone seriously looking to learn PHP should bookmark the site below. If you have any questions they can usually be answered there.

    php.net/

    The function reference is particularly useful.

    php.net/manual/en/funcref.php
    It's a great reference for experienced programmers, but I wouldn't recommend it to people just starting out. It's way too technical and expects you to have an understanding of php.

Page 1 of 4 1234 LastLast

Posting Permissions

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