WELCOME TO PHP!

PHP in Action (1/13)

PHP is a programming language that can do all sorts of things: evaluate form data sent from a browser, build custom web content to serve the browser, talk to a database, and even send and receive cookies (little packets of data that your browser uses to remember things, like if you're logged in to Codecademy).

Check out the code in the editor. Looks familiar, doesn't it? That's because a lot of it is regular old HTML! The PHP code is written in the <?phpand ?>. See how it generates numbers, creates lists, and adds text directly to your webpage?

Why Learn PHP?(2/13)

"So what?" You might say. "I can do that with JavaScript." And that's true! But JavaScript's knowledge can be limited.

JavaScript generally runs in the browser, or client. This means it only really knows what's going on in your browser, plus whatever information it gets from the website(s) you're connecting to.

PHP, on the other hand, runs on the same computer as the website you're visiting, which is known as the server. This means that it has access to all the information and files on that machine, which allows it to construct custom HTML pages to send to your browser, handle cookies, and run tasks or perform calculations with data from that website.

Instructions

We've written a little PHP in the editor to the right, but it's not complete! On line 8, typeMy first line of PHP! between the ""s.

index.php
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>
          <?php
            echo "My first line of PHP!"; 
          ?>
        </p>
    </body>
</html>

PHP and HTML(3/13)

PHP code can be written right into your HTML, like this:

<body>
  <p>

    <?php
      echo "I'm learning PHP!";
    ?>

  </p>
</body>

Your PHP code goes inside the <?php and ?> delimiters. Here we use the functionecho to output I'm learning PHP!. We also end the line with a semicolon.

Instructions

Try it out. On line 8, use echo to output your name. Make sure to end your line with a semicolon.

index.php
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>
          <?php
             echo "Eric";
          ?>
        </p>   
    </body>
</html>

PHP Files(4/13)

You might have noticed that our main file is now index.php instead of index.html. This is important! It tells the PHP interpreter that there's PHP code in the file to evaluate.

Instructions

Ready to learn a few PHP commands? Click Save & Submit Code to get started!

index.php
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>
          <?php
             echo "Eric";
          ?>
        </p>   
    </body>
</html>

Echo(5/13)

The echo function outputs strings. If you type

  echo "Hello!";
?>

PHP will outputHello!.

Make sure to end your line of PHP code with a semicolon.

Instructions

On line 8 in between the<?phpand?>, use echo to output"I'm learning PHP". Make sure to end your PHP code with a semicolon.

index.php
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <h1>
          <?php
             echo "I'm learning PHP";   
          ?>
        </h1>
    </body>
</html>

Strings(6/13)

A string is a word or phrase between quotes, like so:"Hello, world!"

You can type a string all at once, like this:

<?php
  echo "Hello, world!";
?>

Or use the concatenation operator, which glues several strings together:

<?php
   echo "Hello," . " " . "world" . "!";
?>

The concatenation operator is just a dot (.). (If you're coming to PHP from JavaScript, the dot does the same thing for strings that +does in JavaScript.)

Instructions

Go ahead and echo a string of your choice on line 8. Try out the concatenation operator if you're feeling bold!

index.php
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>
          <?php
            echo "Hello," . " " . "world" . "!";
          ?>
        </p>
    </body>
</html>

Arithmetic(7/13)

In addition to outputting strings, PHP can also do math.

<?php
  echo 5 * 7;
?>

Here we use echo to multiply 5 and 7, and we end our line of code with a semicolon. PHP will output 35.

Instructions

On line 8 in between the <?phpand?>, use echo to calculate 17 * 123. Make sure to end your PHP code with a semicolon.

Hint

Instead of

<?php 
  echo "I'm learning PHP!";
?>

we have

<?php 
  echo 17 * 123;
?>
index.php
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>
          <?php
             "I'm learning PHP!"; 
          ?>
          <?php 
              echo 17 * 123;
          ?>
        </p>
    </body>
</html>

Variables(8/13)

So far we've been outputting strings and doing math.

To do more complex coding, we need a way to "save" these values. We can do this using variables. A variable can store a string or a number, and gives it a specific case-senstive name.

Examples:

$myName = "Beyonce";

$myAge = 32;

All variable names in PHP start with a dollar sign ($ ).

Instructions

On line 8, create a variable named $myName and set it equal to your name. Make sure to end your PHP code with a semicolon.

Hint

For example:

<p>

  <?php
   $myName = "Eric";
  ?>

</p>

index.php

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>
          <?php
              $myName = "Eric";
          ?>
        </p>
    </body>
</html>

Semicolons(9/13)

You've probably noticed that our lines of PHP code end in semicolons (;). PHP requires semicolons at the end of each statement, which is the shortest unit of standalone code. (For example,echo "Hello!"; or 2 + 2;)

You can think of a statement is a complete PHP thought. 19 + orechoaren't complete thoughts, so you wouldn't put semicolons at the end of them!

<?php echo "Use your semicolons!"; ?>

Instructions

We've accidentally forgotten our semicolon on line 8. Add it in!

index.php
<!DOCTYPE html>
<html>
    <head>
        <title>Oh No!</title>
    </head>
    <body>
        <p><?php
            echo "Oh, the humanity!";
          ?></p>
    </body>
</html>

Comments(10/13)

Just like we sometimes put comments in our CSS (using /* this syntax */) or in our HTML (using<!-- this syntax -->), we can also put comments in our PHP code! We do that using two forward slashes (//), like so:

<?php
    echo "I get printed!";
    // I don't! I'm a comment.
?>

Instructions

Go ahead and add a comment to our PHP code. It can say whatever you like!

index.php
<!DOCTYPE html>
<html>
    <head>
        <title>Oh No!</title>
    </head>
    <body>
        <p><?php
            echo "Oh, the humanity!";
            // I don't! I'm a comment.
          ?></p>
    </body>
</html>

Creating a Variable(11/13)

Practice makes perfect! Let's get started by creating a variable and giving it a value.

Instructions

Declare a variable, $myName, and give it your name as a string.

Hint

For example, you might type:

<?php $myName = "Eric"; ?>

index.php
<!DOCTYPE html>
<html>
    <head>
        <link type='text/css' rel='stylesheet' href='style.css'/>
        <title>PHP FTW!</title>
    </head>
    <body>
        <!-- Write your PHP code below!-->
        <p>
           <?php $myName = "Eric"; ?>
        </p>   
    </body>
</html>

Let the Machine Do the Math(12/13)

Remember, computers were built to do the stuff we don't want to! And that includes doing tedious computations.

Instructions

After your first variable, declare a second, $myAge, and set it equal to your age as a number. Remember: no quotes around numbers!

Hint

Your code should now look something this:

<?php $myName = "Eric";
      $myAge = 26; ?>
index.php
<!DOCTYPE html>
<html>
    <head>
        <link type='text/css' rel='stylesheet' href='style.css'/>
        <title>PHP FTW!</title>
    </head>
    <body>
        <!-- Write your PHP code below!-->
        <p>
           <?php $myName = "Eric";
                 $myAge = 26; ?>
        </p>   
    </body>
</html>

Echo It! (13/13)

Nice work! PHP computed the value for you, but it didn't appear in your .php document because we didn't print it using echo. Let's fix that!

Instructions

Let's finish this up! Beneath your existing PHP code, use echo to print out your name and your age, like so:

echo $myName;
echo $myAge;
index.php
<!DOCTYPE html>
<html>
    <head>
        <link type='text/css' rel='stylesheet' href='style.css'/>
        <title>PHP FTW!</title>
    </head>
    <body>
        <!-- Write your PHP code below!-->
        <p>
           <?php $myName = "Eric";
                 $myAge = 26; ?>
                 echo $myName;
                 echo $myAge;
        </p>   
    </body>
</html>