Let's Write Some PHP

Video loading...

  • 0:14
    Let's Write Some PHP with Leanna Pelham.
  • 0:26
    LEANNA PELHAM: Welcome.
  • 0:27
    We're glad you're here with us to learn
  • 0:29
    how to become an epic PHP developer.
  • 0:31
    PHP is a programming language that runs a large percentage of the web,
  • 0:35
    including site as big as Facebook.
  • 0:38
    But since PHP has been around for a while,
  • 0:40
    there's a lot of bad, outdated and boring
  • 0:42
    information about it on the web.
  • 0:44
    But not here.
  • 0:45
    In this course, we'll learn PHP from scratch by building a real website.
  • 0:49
    This means you'll learn the practices
  • 0:51
    used by real, employed developers to build really cool things
  • 0:55
    and not just a bunch of theory.
  • 0:57
    We'll teach you something in each chapter
  • 0:59
    and then you'll test and practice your new skills
  • 1:01
    by coding right in your browser.
  • 1:03
    Learn then practice.
  • 1:04
    That's the key.
  • 1:05
    Before long, you'll be creating more and more complex things
  • 1:08
    and be the coolest guy or gal that any of your friends knows.
  • 1:12
    Probably.
  • 1:13
    We're going to build a site that we're calling airpupandmeow.com.
  • 1:16
    Imagine a site like Airbnb except where people
  • 1:20
    rent cute pets instead of apartments.
  • 1:22
    If you're looking for companionship or something
  • 1:24
    to take to the dog park, without all that responsibility of walking
  • 1:28
    your dog every morning and bringing a bag to pick up his gifts,
  • 1:32
    then this site would be for you.
  • 1:34
    OK.
  • 1:35
    The idea might be kind of silly, but that
  • 1:37
    hasn't stopped start-ups in the past.
  • 1:38
    So let's go.
  • 1:40
    What you see here is just an HTML page
  • 1:43
    that I've loaded in my browser.
  • 1:44
    This is a template based on Twitter Bootstrap
  • 1:47
    and it's just a bunch of hard coded text
  • 1:48
    and links that don't go anywhere yet.
  • 1:51
    But it's already a cute start to our rent a pet site.
  • 1:54
    For now, don't worry about the local host part that I have
  • 1:56
    in the URL or why this file ends in .PHP.
  • 2:00
    Just know that when I load this page,
  • 2:01
    the index.php file is being opened and all
  • 2:04
    the HTML is rendered by my browser.
  • 2:07
    I'm going to use my favorite editor called PHP Storm to open this file
  • 2:10
    and prove that it's only simple HTML.
  • 2:15
    Later in this course we'll get your computer set up
  • 2:17
    to run and modify files just like this.
  • 2:21
    Right now this page is totally static.
  • 2:24
    Each time I refresh the page, I get back the exact same HTML.
  • 2:28
    On real websites, things they're dynamic.
  • 2:30
    New stories update when I refresh and personalized information
  • 2:33
    is pulled from a database.
  • 2:35
    That's the kind of stuff that PHP does.
  • 2:37
    Let's make this page more interesting.
  • 2:40
    Before you write PHP code, you'll always
  • 2:42
    start with the same opening tag.
  • 2:45
    This is what tells PHP that we're not writing HTML anymore.
  • 2:49
    We actually want to write some PHP code.
  • 2:52
    Let's print out a cool message by using the PHP Echo statement
  • 2:55
    and surrounding our message with single quotes.
  • 2:58
    Finish off the line with a semicolon and then write the PHP closing tag.
  • 3:02
    These last two characters get us out of PHP mode and back into HTML.
  • 3:07
    The less than question mark PHP and question mark greater than tags
  • 3:10
    are exact opposites and always come in a pair.
  • 3:14
    One gets us into PHP mode and the other exits PHP mode.
  • 3:18
    Before we talk about what we did, let's
  • 3:19
    celebrate because when I refresh the page it works.
  • 3:23
    PHP is printing our message in the middle of the page.
  • 3:26
    The key is the PHP Echo statement, whose job is to print things out.
  • 3:30
    The message itself is called a string,
  • 3:32
    and strings are always surrounded by single quotes when you write them.
  • 3:36
    Since printing a static string is boring, let's create a variable.
  • 3:39
    Whenever we want to write PHP code, remember
  • 3:42
    to open up PHP with less than question mark PHP
  • 3:45
    and then close it with question mark greater than.
  • 3:48
    The open and close PHP tags can totally be on separate lines.
  • 3:52
    If we refresh now there's no change.
  • 3:54
    Unless we print something from within PHP,
  • 3:57
    nothing is shown on the page.
  • 3:58
    Even if we add blank lines, they don't
  • 4:00
    appear inside each HTML source code.
  • 4:12
    To create a variable, start with the dollar sign, write a clever name,
  • 4:16
    then finish it up with an equal sign and the value
  • 4:20
    we want to give or assign to the variable.
  • 4:23
    Remember to add a semicolon at the end of the line.
  • 4:26
    Almost all lines in PHP end in a semicolon.
  • 4:29
    Did you hear me?
  • 4:30
    Because forgetting this is one of the most common errors you'll make.
  • 4:33
    I promise.
  • 4:35
    If we refresh nothing changes yet.
  • 4:37
    That makes sense because we haven't printed anything from within PHP.
  • 4:41
    Using the variable is easy.
  • 4:43
    Replace your echoed string with a dollar sign and the variable name.
  • 4:46
    And just like that, we're creating and using variables
  • 4:49
    and one step closer to your new best friend.
  • 4:59
    Of course variables can be set to numbers which
  • 5:01
    look the same but without the quotes.
  • 5:04
    Notice that I have two PHP lines or statements inside one
  • 5:07
    set of opening and closing PHP tags.
  • 5:10
    That's totally legal.
  • 5:12
    Once you open PHP, you can write as much as you want.
  • 5:16
    Use your new variable to print another message.
  • 5:32
    When we refresh, it's a success.
  • 5:35
    Now let's make a small error to see what happens.
  • 5:38
    I'll just remove the semicolon from the end
  • 5:40
    of the welcome message line.
  • 5:44
    You'll see a lot of error messages and the trick
  • 5:46
    is to get good at knowing what they mean.
  • 5:48
    Be sure to look at the line number and check the line and the lines
  • 5:51
    above it.
  • 5:52
    In this case, the error is being reported
  • 5:54
    in the line with pup count.
  • 5:56
    But there's nothing wrong with this line.
  • 5:58
    The missing semicolon is actually in the line above this.
  • 6:03
    That's really common with PHP errors.
  • 6:05
    So look for it.
Loading ...

Let's Write Some PHP

Loading...

So you want to become a PHP developer. And not just any ole' programmer, but someone who writes great code and can work on exciting projects. Well then, thisĀ  series from our partners at KnpUniversity is for you! You'll learn how to develop with PHP from the very beginning, with a real project, and coding exercises throughout the lessons so you can practice immediately.

In this series, we'll learn PHP from scratch by building a real website. This means you'll learn the practices used by developers to build really cool things, and not just a bunch of theory. We're going to build a site that we're calling AirPupnMeow.com. Imagine a site like Airbnb.com, except where people rent cute pets instead of apartments. If you're looking for companionship without all that responsibility of walking your dog every morning and bringing a bag to pick up his... uh gifts, then this site would be for you! Ok, the idea might be kinda silly, but that hasn't stopped startups in the past!

In this first lesson we are going to dive right in and create our first PHP file, play around with variables, and also look at what happens when we make an error, and how to read the error message we get. If you want to follow along with the steps here, you will need access to a web sever where you can write PHP files. You can get set up quickly with a number of local development servers. We have lessons that cover material for different operating systems in our Installing a Local Development Environment guide.

Throughout this first series of PHP for Beginners, you are going to learn to:

  • Use functions, and variables
  • Work with arrays, loops, and if statements
  • Read and update files
  • Handle JSON
  • Set up your computer to run PHP
  • Create your own functions
  • Organize your project into multiple files
  • Create a simple layout
Downloads: 
Log in or sign up to download companion files.