Download Article
The ultimate beginner's guide to writing PHP scripts
Download Article

PHP is a server scripting language used to make web pages interactive. It became widely popular due to its ease of use, interactivity within web pages, and integration with HTML. Think of what happens when a page is edited on this website. Behind this process are many, perhaps hundreds, of PHP scripts controlling how web pages change based on a variety of circumstances. This article will teach you how to write a few very simple PHP scripts so that you can get a basic understanding of how PHP works.

Things You Should Know

  • You can use echo statements in your PHP script to display HTML content in a web browser.
  • When combining PHP code and HTML in a single file, save the file with the .php file extension.
  • All variables in PHP begin with a $ character.
Part 1
Part 1 of 3:

Echo Statements

Download Article
  1. How.com.vn English: Step 1 Open a text editor.
    This is the program you will be using write and edit your code.
    • Notepad can be accessed on any version of Windows using Win + R > Notepad.
    • TextEdit can be accessed on Mac by going to Applications > TextEdit.
  2. How.com.vn English: Step 2 Type a simple statement into Notepad.
    A section of PHP code begins and ends with bracketed PHP tags (“<?php” “?>”). “Echo” is a very basic statement (an instruction to the computer) in the PHP language that will output text to the screen. The text you want to echo must be enclosed in quotation marks and end in a semi-colon.
    • The code should look something like <?php echo “Hello World!”; ?>.
    Advertisement
  3. How.com.vn English: Step 3 Save the file with name “hello world” and the extension .php.
    This is done by navigating to File > Save As...
    • In Notepad, add .php to the end of the filename and enclose in double quotations. This ensures the file will not be converted into a basic text file by Notepad. Without the quotation marks, the file will become hello world.php.txt. Alternatively, you can select the drop down menu under Save as type and change it to "All Files (*.*)" which will leave the name exactly how you type it and the quotes will not be needed.
    • In TextEdit, no quotations marks are necessary, but a popup will appear asking you to verify that you want the file saved as .php.
    • Make sure you save the file to your “server’s” document root directory. Typically this is the folder named “htdocs” in your Apache folder on Windows, or /Library/Webserver/Documents on Mac, but can be set by the user manually.
  4. How.com.vn English: Write PHP Scripts Step 4
    4
    Access the PHP file with a web browser. Open your preferred web browser and type this address in the address bar using the name of your php file: http://localhost/hello world.php. Your browser window should display the echo statement.
    • If you receive an error message, make sure you typed the code correctly as shown above, including the colon.
    • Also make sure that your file is saved into the correct directory.
  5. Advertisement
Part 2
Part 2 of 3:

Using PHP & HTML

Download Article
  1. How.com.vn English: Step 1 Understand the ‘php’ tags.
    The “<?php” and “?>” tags tell the PHP engine that everything between them is PHP code. Everything outside the two tags is treated as HTML and ignored by the PHP engine and sent to your browser the same as any other HTML. The important thing to recognize here is that PHP scripts are embedded inside regular HTML pages.
  2. How.com.vn English: Step 2 Understand the statement between the tags.
    Statements are used to tell the PHP engine to do something. In the case of an echo statement, you are telling the engine to print what is inside the quotes.
    • The PHP engine itself never actually prints anything to your screen. Any output generated by the engine is sent to your browser as HTML. Your browser does not know that it's getting PHP output. As far as the browser is concerned, it's getting plain HTML.
  3. How.com.vn English: Step 3 Use HTML tags to make your statement bold.
    Adding HTML tags can alter the output of the php statement. The “<strong>” “</strong>” tags will add bold formatting to any text placed inside of them. Note that these tags appear on the outside of the text, but inside of the quotations marks of the echo statement.
    • You want your code to look something like:
      <?php?
      echo "<strong>Hello World!</strong>";
      ?>
  4. How.com.vn English: Step 4 ...
    Save and open the file in the browser. Go to File > Save As… and save the file as "helloworld2.php”, and open it in your browser by using the address: http://localhost/helloworld2.php. The output is the same as before, but this time the text is in bold.
    • Make sure you save the file to your “server’s” document root directory. Typically this is the folder named “htdocs” in your Apache folder on Windows, or /Library/Webserver/Documents on OSX, but can be set by the user manually.
  5. How.com.vn English: Step 5 Edit the file to add a second echo statement.
    Remember, statements need to be separated by a semicolon.
    • Your code should look something like:
      <?php
      echo “Hello World!”<br>;
      echo “How are you doing?”;
      ?>
  6. Step 6 Save and run the file as "hello world double.php".
    The page will display two echo statements, listed in order, on two lines. Notice the “<br>” on the first line. This is HTML markup to insert a line break.
    • If you didn't add this, your output would look like this:
      Hello World!How are you doing?
  7. Advertisement
Part 3
Part 3 of 3:

Variables

Download Article
  1. How.com.vn English: Step 1 Think of variables as containers for data.
    To manipulate data, be it numbers or names, you need to store the data in a container. This process is called declaring the variable. The syntax for declaring a variable is “$myVariable = “Hello World!”;”
    • The dollar sign ($) at the beginning tells PHP that $myVariable is a variable. All variables must start with the dollar sign, but the name of the variable can be anything.
    • In the above example, the value is "Hello World!", and the variable is $myVariable. You're telling PHP to store the value at the right of the equal sign, into the variable at the left of the equal sign.
    • A variable containing a text value is known as a string.
  2. How.com.vn English: Step 2 Call the variable.
    Referring to a variable in the code is known as a call. Declare your variable, then echo the variable instead of typing out the text.
    • Your code might look something like:
      <?php>
      $myVariable = “Hello World!”;
      echo $myVariable;
      ?>
  3. How.com.vn English: Step 3 ...
    Save and run the file. Go to File > Save As… and save the file as “myfirstvariable.php”. Open your browser and navigate to http://localhost/myfirstvariable.php and the script will print the variable. The output looks the same as printing plain text, but how it was achieved is different.
    • Make sure you save the file to your “server’s” document root directory. Typically this is the folder named “htdocs” in your Apache folder on Windows, or /Library/Webserver/Documents on OSX, but can be set by the user manually.
  4. How.com.vn English: Step 4 Use variables with numbers.
    Variables can also contain numbers (known as integers), and then those numbers can be manipulated using simple mathematical functions. Start by declaring three variables called “$mySmallNumber”, “$myLargeNumber”, and “$myTotal”.
    • Your code should look something like:
      <?php
      $mySmallNumber;
      $myLargeNumber;
      $myTotal;
      ?>
  5. How.com.vn English: Step 5 Assign integer values to the first two variables.
    Give an integer value to “$mySmallNumber” and “myLargeNumber”.
    • Note that integer values do not need to be contained in quotation marks. That will cause numbers to be treated as a text value like the “Hello World!” variable.
    • Your code should look something like:
      <?php
      $mySmallNumber = 12;
      $myLargeNumber = 356;
      $myTotal;
      ?>
  6. How.com.vn English: Step 6 Use the third variable to calculate and print the sum of the other variables.
    Rather than doing the math yourself, you can call the two variables in the “$myTotal” variable. Using a mathematical function, the machine will calculate the sum for you. To print the variable, you need only add an echo statement that calls the variable after the declaration.
    • Any change to either integer variable would be reflected when printing the “$myTotal” variable with echo.
    • Your code should look something like:
      <?php
      $mySmallNumber = 12;
      $myLargeNumber = 356;
      $myTotal = $mySmall Number + $myLargeNumber;
      echo $myTotal;
      ?>
  7. How.com.vn English: Step 7 Save the file and run this script.
    Your browser window will display a single number. That number is the sum of the two variables called in the “$myTotal” variable.
  8. How.com.vn English: Step 8 Review your string variables.
    Using a variable to store text allows you to call that variable any time you want to use the store value instead of constantly typing out the contained text. It also allows for more complex manipulation of the stored data moving forward.
    • The first variable, $myVariable, contains a string value; "Hello World!". Unless you change the value, $myVariable will always contain the value "Hello World!".
    • The echo statement prints the contained value of $myVariable.
  9. How.com.vn English: Step 9 Review your integer variables.
    You have explored basic manipulation of integer variables by using a mathematical function. The resulting data can be stored into another variable. This is only the beginning of what can be accomplished with these variables.
    • The two variables, $mySmallNumber, and $myLargeNumber are each assigned an integer value.
    • The third variable, $myTotal, stores the added values of $mySmallNumber and $myLargeNumber. Since $mySmallNumber holds one numeric value, and $myLargeNumber holds a second numeric value, this means $myTotal holds the value of the first number added to the second number. This value can change with alterations to either of the included variables.
  10. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      Tips

      • This article assumes you've installed Apache and PHP on your computer. Anytime it's said to save a file, you are saving in the "\ht docs" (Win) or “\Library\WebServer\Documents” (Mac) directory inside the Apache directory.
      • Commenting is important with any programming, so make sure you know how to comment in PHP as well.
      • A really useful tool to help you test PHP files is XAMPP which is a free program that installs and runs Apache and PHP to help you simulate a server on your computer.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      Things You'll Need

      • The Apache Web Server (Win32)
      • PHP (Win32)
      • A Text Editor(Choose one from below)
        • Windows Notepad
        • Notepad++ (Win) (Has syntax highlighting for easier reading)
        • Textwrangler (Mac) (Has similar capabilities as Notepad++)
      • HTML editors(Choose one from below)
        • WYSIWYG
        • Adobe Dreamweaver
        • Microsoft Expression Web
        • Some IDEs like Microsoft Visual Studio Web.
      • Mozilla Firefox (Any browser will work, but Mozilla is a popular choice amongst web developers)
      • For basic users try XAMPP.(A free program that makes your computer a server with PHP, Perl, and many addons including Python)

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about programming, check out our in-depth interview with Tyrone Showers.

      About This Article

      How.com.vn English: Tyrone Showers
      Co-authored by:
      Technologist
      This article was co-authored by Tyrone Showers. Tyrone Showers is a Technologist and the Co-owner of Taliferro Group, an IT consulting company based in Seattle, Washington. With over 35 years of professional experience, he specializes in API Design, e-Commerce, Operational Efficiency, and website development. He has a B.S. in Computer Science from DeVry Institute of Technology. This article has been viewed 538,485 times.
      How helpful is this?
      Co-authors: 79
      Updated: February 21, 2024
      Views: 538,485
      Categories: PHP
      Thanks to all authors for creating a page that has been read 538,485 times.

      Reader Success Stories

      • How.com.vn English: Sudharshan As

        Sudharshan As

        Aug 23, 2016

        "It's a good recap for me. How.com.vn is best on showing the pic-shots as example. I love to use it. Thanks for..." more
      Share your story

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement