Introduction

Welcome!

Heya! In this course, we'll master PHP, a programming language that's used by millions of websites.

Let's hit the ground running and check out what this little PHP script does.

<?php
 echo "Welcome, friend!";
?>

//Output Below

Ah! In this lesson, we'll find out what we just did here.

Capabilities

PHP can do tons of things, making it a great choice for almost any website.

What might these things include?

  • It can take user input from forms
  • It can talk to databases
  • We can build dynamic webpages with it

Yes! That's why even big companies like Facebook and Tesla use PHP.

Tags

PHP programs, or scripts as they're called, start with an opening tag and end with a closing tag.

Let's see the two tags in a script.

<?php
 echo "Welcome, friend!";
?> 
  • <?php and ?>

That's right! These tags form the beginning and the end of every script.

Echo-echo-echo

Do you remember the special word we used to display "Welcome, friend!"?

<?php  
echo "Welcome, friend!";
?>

//Output Below

That's it! The echo keyword displays, or prints, the plain text we put in the quotation marks after it.

Semicolons

Also, there's a special symbol at the end of every line.

What kind of symbol was that again?

<?php
 echo "Welcome, friend!";
?>

//Output Below

Yes! The semicolon tells the server that it can move to the next line.

Numbers

Apart from plain text, however, PHP can also handle numbers and do math with them.

<?php
 echo 2 * 4; 
?>

//Output Below

Magical! The * sign is an operator that multiplies two numbers. The +  operator unsurprisingly, will add two numbers. 

Psst: numbers have no quotation marks around them.

Variables

To create some more complex scripts, we need a way to remember information. So-called variables help us do just that.

<?php
$name = "Slim Shady";
 echo $name;
?>

//Output Below

Great! Variables start with a $ sign and have a name. We can pass them information using the = sign.

Comments

Comments are lines that can help us understand a script. We introduce them with two slashes.

<?php
// Create a variable and display its content:
 $name = "Slim Shady";
 echo $name;
?>

//Output Below

Perfect! When a line starts with //, it's ignored by the server.

A server-side language

Now, PHP is a so-called server-side language, which makes it different from languages like JavaScript.

Why might that be?

  • Any PHP script needs to run on a server
  • Only the results of a script are displayed in the browser

That's it! PHP scripts run on a server, which is the computer that stores them. Only the results are delivered to browser.

PHP and HTML

When we combine PHP with HTML, we can create dynamic webpages.

<html>
 <body>
  <?php
   echo "Hi, I'm written in PHP";
  ?>
 </body>
</html>

//Output Below

Yay! When we open this webpage, the server processes the script and sends only the output to the browser.

PHP files

If we include a PHP script in an HTML file, we need to save it with a .php extension.

Why might that be?

  • The extension tells the server that it needs to process the file

Right! The .php extension tells the server that there's PHP code in the file that it needs to process.