Basic PHP Syntax: Free PHP & MySQL Tutorial

This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Note: These materials are provided to give prospective students a sense of how we structure our class exercises and supplementary materials. During the course, you will get access to the accompanying class files, live instructor demonstrations, and hands-on instruction.

Topics covered in this PHP & MySQL tutorial:

Echo, strings, & variables, Single quotes vs. double quotes, Escaping characters, Heredoc, Concatenation, Comments

Exercise Overview

This exercise gets you started with the basics of PHP syntax. If you’re familiar with a language like JavaScript, a lot of this will look familiar to you, but even if you do not have previous programming experience, you’ll find that PHP is pretty simple to learn.

Mac: Launching MAMP PRO

  1. If MAMP PRO is not already running, go to Hard Drive > Applications > MAMP PRO and double–click MAMP PRO.app.

  2. MAMP PRO will launch and it may automatically open the MAMP start page in your default web browser.

    If it does not automatically open, you can click the WebStart button in the MAMP PRO application (you may need to click the Start button first), or in a browser go to localhost:8888/MAMP

Windows: Launching XAMPP

  1. If XAMPP is not already running, navigate to the C: > xampp folder, then double–click xampp-control.exe.

  2. Next to Apache click Start.

  3. Next to MySQL click Start.

Echo, Strings, & Variables

  1. In your code editor, open first.php from the phpclass folder. This folder is located in your htdocs folder here:

    • Mac: Hard Drive > Applications > MAMP > htdocs
    • Windows: C: > xampp > htdocs
  2. In between the <body> tags, add the following bold code:

    <body>
    <?php 
       echo "Hello Universe";
    ?>
    </body>
    

    Let’s break down this very basic piece of code. We use <?php and ?> to delineate the PHP code from the rest of the page. Everything inside will be PHP, and everything outside will be plain HTML.

    The echo command simply prints something to the page. Here we have put a string in double quotes, so it just outputs that string.

    The ; signifies the end of the PHP statement. It is very important not to forget the semi-colon or you may get errors in your code.

  3. Save the page.

  4. Open a browser and go to:

    • Mac: localhost:8888/phpclass/first.php
    • Windows: localhost/phpclass/first.php

    The page should read:

    Hello Universe

  5. The echo command will also do simple math. Switch back to your code editor.

  6. Delete everything between the <?php ?> tags and add the following bold code:

    <?php 
       echo 2 + 2;
    ?>
    

    Notice that we did not put quotes around 2 + 2. This tells PHP to evaluate the numbers as an expression, rather than a string.

  7. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/first.php
    • Windows: localhost/phpclass/first.php

    The page should read:

    4

  8. Leave the page open in the browser so we can come back to it after making changes.

  9. Let’s see what happens when we add quotes. Switch back to your code editor.

  10. Add the quotes as shown in bold:

    <?php 
       echo "2 + 2";
    ?>
    
  11. Save the page, go to the browser, and reload first.php.

    The page should read:

    2 + 2

    The quotes tell PHP to evaluate it as a string of text.

  12. Now let’s make a variable. Switch back to your code editor.

  13. Select everything between the <?php ?> tags and replace it with the code shown in bold:

    <?php 
       $myMessage = "Hello Universe";
       echo $myMessage;
    ?>
    

    Don’t forget the semi-colons at the end of each line!

  14. Save the page, go back to the browser, and reload first.php.

    The page should read:

    Hello Universe

    You just created a variable called $myMessage and printed it to the page. All PHP variables start with a $ sign.

    NOTE: Variables are also case-sensitive, so $mymessage and $myMessage would be totally unrelated!

Single Quotes vs. Double Quotes

You may have noticed that we have been using double quotes for our strings. We could also use single quotes, but there are a few subtle differences.

  1. Switch back to your code editor.

  2. Add single quotes around $myMessage as shown in bold:

    $myMessage = "Hello Universe";
    echo '$myMessage';
    
  3. Save the page, go to the browser, and reload first.php.

    The page should read:

    $myMessage

    The single quotes tell PHP to evaluate everything inside them as a literal string of characters.

  4. Now let’s try the same thing but with double quotes. Switch back to your code editor.

  5. Add double quotes around $myMessage as shown in bold:

    $myMessage = "Hello Universe";
    echo "$myMessage";
    
  6. Save the page, go to the browser, and reload first.php.

    The page should read:

    Hello Universe

    Wow, it evaluated the variable. Double quotes are a bit more flexible and will allow you to mix variables and strings all at once. Let’s try it out.

  7. Switch back to your code editor.

  8. Add the following code shown in bold:

    $myMessage = "Hello Universe";
    echo "$myMessage, nice to meet you.";
    
  9. Save the page, go to the browser, and reload first.php.

    The page should read:

    Hello Universe, nice to meet you.

Escaping Characters

  1. Switch back to your code editor.

  2. Select everything between the <?php ?> tags and replace it with the following bold code:

    <?php 
       echo 'It's nice to meet you.';
    ?>
    
  3. Save the page, go to the browser, and reload first.php.

    The page will error out and not work. Why? PHP sees the single quote character in It’s as the end of the string, and then it gets confused by all the characters after it. We must instead escape the single quote.

  4. Switch back to your code editor.

  5. Add the following \ shown in bold:

    echo 'It\'s nice to meet you.';
    
  6. Save the page, go to the browser, and reload first.php.

    The page should read:

    It’s nice to meet you.

  7. We can do the same thing with double quotes. Switch back to your code editor.

  8. Edit the line so it reads:

    echo "Tell him I said \"Hi\".";
    
  9. Save the page, go to the browser, and reload first.php.

    It should read:

    Tell him I said “Hi”.

    There are other escape characters you can use, such as \\ for a backslash itself, \t for a tab, \n for a new line, and \r for a carriage return.

Concatenation

There are a few ways to concatenate text and variables. (Concatenate means to “link things together in a chain or series.”)

  1. Switch back to your code editor.

  2. Select everything between the <?php ?> tags and replace it with the following bold code:

    <?php 
       echo "This ". "is ". "my string.";
    ?>
    

    Pay careful attention to the spaces when you type this!

  3. Save the page, go to the browser, and reload first.php.

    It should read:

    This is my string.

  4. The period syntax is a little bit more flexible. With it we can append something directly to a variable. Switch back to your code editor.

  5. Select everything between the <?php ?> tags and replace it with the following bold code:

    <?php 
       $msg = "I like carrots.";
       $msg .= " And broccoli.";
       echo $msg;
    ?>
    
  6. Save the page, go to the browser, and reload first.php.

    It should read:

    I like carrots. And broccoli.

    The . appends the string to the variable. This is a useful shorthand and you will see it often.

Comments

  1. Switch back to your code editor.

  2. To comment out a single line of code, add // to the beginning of the line, as shown below in bold:

    $msg = "I like carrots.";
    //$msg .= " And broccoli.";
    echo $msg;
    

    In most code editors, the middle line will gray out, indicating that it is a comment and will no longer be executed.

  3. To comment out multiple lines, use /* and */ as shown in bold below:

    /*$msg = "I like carrots.";
    //$msg .= " And broccoli.";
    echo $msg;*/
    

    Now the entire block of code will gray out indicating it is one big comment.

  4. Close the file, saving changes if you wish. We’re done with it.

Yelp Facebook LinkedIn YouTube Twitter Instagram