PHP For Beginners, Ep 3 - Your First PHP Tag

Laracasts
12 Sept 202208:10

Summary

TLDRIn this instructional video, the presenter guides viewers through setting up their first website using PHP. They start by creating a directory for the site, then proceed to build a basic HTML file named 'index.html'. The presenter emphasizes the importance of the 'index' filename for web servers. After demonstrating a simple 'Hello World' example, they transition to PHP by renaming the file to 'index.php' and introducing PHP blocks to create dynamic content. The tutorial concludes with an encouragement to practice PHP by echoing different strings across the document, aiming to familiarize users with PHP syntax and functionality.

Takeaways

  • 🛠️ The video is a tutorial on setting up the initial steps for creating a website using PHP.
  • 📂 The process begins by creating a directory for the website in the user's home directory, which can vary based on the operating system and the method of PHP installation.
  • 🍻 If installed via Homebrew, websites can be stored anywhere, but other methods like MAMP might require specific directories.
  • 📁 The user creates a 'websites' directory and then a 'demo' directory within it to house the project files.
  • 🖥️ The user opens their chosen code editor to start working on the project files, emphasizing the importance of seeing only the project files for focus.
  • 📝 The first file created is 'index.html', starting with basic HTML boilerplate and simplifying it for the tutorial.
  • 🌐 The user mentions that understanding of HTML and CSS is assumed, suggesting to research basics if unfamiliar.
  • 🔗 The video demonstrates how to view the HTML file in a browser, using PHP's built-in server with the command 'php -S localhost:8888'.
  • 🔄 The importance of the 'index' filename is highlighted, as it is the default page served by the server.
  • 📝 The tutorial transitions from static HTML to dynamic PHP by renaming 'index.html' to 'index.php'.
  • 🔑 The user introduces PHP syntax, explaining how to create a PHP block and use 'echo' to output strings.
  • 🎓 The tutorial concludes by encouraging practice with PHP 'echo' to become comfortable with the syntax and its usage in a webpage.

Q & A

  • What is the first step mentioned in the script for setting up a new website?

    -The first step is to open the terminal that was set up in the previous episode and create a directory for the new website.

  • How does the method of creating a new site depend on the installation of the environment?

    -The method of creating a new site depends on how you installed PHP and your server environment. For example, if you installed PHP using Homebrew, you can store website directories anywhere, but if you used MAMP, you should follow the specific directory instructions provided in the documentation.

  • What command is used to create a new directory in the terminal?

    -The command used to create a new directory is 'mkdir' followed by the name of the directory.

  • What is the significance of the 'index' filename in a web server context?

    -The 'index' filename is significant because it is the default file name that a web server looks for when serving a directory. If the index file is renamed, the server won't be able to find it upon a refresh.

  • How can you view the results of your HTML file directly in a browser?

    -You can view the results by either using the inline viewing functionality of your editor or by loading the file directly in a browser using the file's URL.

  • What command is used to start PHP's built-in web server?

    -The command to start PHP's built-in web server is 'php -S' followed by the server's hostname and port number, for example, 'php -S localhost:8888'.

  • Why is it recommended to stick with 'echo' instead of 'print' when writing PHP?

    -While both 'echo' and 'print' can be used to output strings in PHP, 'echo' is generally preferred as it is more commonly used and has some slight differences that make it more suitable for outputting strings.

  • What is the purpose of the PHP opening and closing tags in a PHP file?

    -The PHP opening and closing tags are used to designate that the code between them should be treated as PHP code, not HTML, allowing for dynamic content generation.

  • What should you do if the HTML content looks foreign to you according to the script?

    -If the HTML content looks foreign, it is recommended to pause the series and research HTML basics, such as 'HTML for Dummies', to get comfortable with HTML and CSS before proceeding with the PHP course.

  • What is the easiest way to print a string in PHP as mentioned in the script?

    -The easiest way to print a string in PHP is by using the 'echo' statement followed by the string in quotes and ending with a semicolon.

Outlines

00:00

📁 Setting Up the Development Environment

The speaker begins by ensuring that the tools for web development are in place and proceeds to guide the audience through setting up a directory for their first website. The process varies depending on the environment setup, such as using Homebrew or MAMP, and the speaker emphasizes checking the documentation for the specific installation path. They demonstrate creating a 'websites' directory in the home folder and then a 'demo' directory within it. The audience is instructed to open their chosen code editor and create a basic 'index.html' file with simple HTML boilerplate. The speaker also advises that if the HTML seems unfamiliar, the audience should first learn the basics of HTML and CSS before proceeding with the series.

05:00

🔧 Transitioning to PHP for Dynamic Content

The second paragraph focuses on transitioning from a static HTML page to a dynamic one using PHP. The speaker renames the 'index.html' file to 'index.php' and explains that while PHP's built-in web server will still serve the page, it now has the capability to process PHP code. The audience is guided to create a PHP block within the file and use the 'echo' statement to print 'Hello World'. The speaker clarifies that 'echo' is used to output text onto the page and is similar to the 'print' function, with minor differences. The goal is to become comfortable with PHP syntax and to practice inserting PHP blocks in various parts of the document to output different strings. The importance of the 'index' filename for default page serving is also highlighted.

Mindmap

Keywords

💡Tools

In the context of the video, 'tools' refer to the software and utilities necessary for web development, such as a code editor and a server environment. The script emphasizes that having these tools in place is a prerequisite for starting the project, indicating their fundamental role in the web development process.

💡Browser

The 'browser' is the application used to access and view web content. In the script, the browser is the medium through which the viewer will see the results of the web development process, highlighting its importance in testing and displaying web pages.

💡Terminal

The 'terminal' is a command-line interface for interacting with the operating system. In the script, the terminal is used for executing commands to set up the development environment and to start the web server, demonstrating its utility in web development tasks.

💡Directory

A 'directory' is a folder used to organize files on a computer. The script instructs the viewer to create a directory for their website, illustrating the importance of file organization in web development projects.

💡PHP

PHP is a server-side scripting language used for web development. The script discusses the installation of PHP and its use in creating dynamic web content, underscoring its role as a core technology for building websites.

💡HTML

HTML, or HyperText Markup Language, is the standard markup language for creating web pages. The script begins by creating a simple HTML file, 'index.html', to demonstrate the transition from static to dynamic web content using PHP.

💡Boilerplate

A 'boilerplate' is a template or pre-configured code that provides a starting point for a project. The script mentions using boilerplate for an HTML file to simplify the setup process and ensure a standard structure.

💡Echo

In PHP, 'echo' is a language construct used to output one or more strings. The script uses 'echo' to demonstrate how to display dynamic text on a web page, highlighting its use in generating content.

💡PHP Block

A 'PHP block' is a section of code enclosed within opening and closing PHP tags. The script introduces the concept of PHP blocks to differentiate PHP code from HTML, showing how to integrate PHP into a web page.

💡Development Server

A 'development server' is a local server used for testing and development purposes. The script explains how to start a PHP development server to preview the web page in a browser, emphasizing the importance of a server for running PHP scripts.

💡Index File

The 'index file' is the default file that a web server looks for when accessing a directory. In the script, the importance of the 'index' naming convention is discussed, as it ensures the server serves the correct file when the directory is accessed.

Highlights

Setting up tools for creating a website and starting with a simple step in the browser.

Creating a directory for the first website based on the user's environment setup.

Using Homebrew for PHP installation allows flexibility in choosing website directory locations.

Different installation paths may require specific directory structures for websites.

Navigating to the home directory on Mac using the tilde and forward slash.

Creating a 'websites' directory to organize all website projects.

Naming the first project 'demo' and creating a directory for it.

Opening the chosen editor to start working on the project files.

Creating a new file 'index.html' for a basic static HTML page.

Simplifying the HTML boilerplate by removing unnecessary meta tags.

Recommendation to learn HTML basics before proceeding with the series.

Using the terminal to start a PHP built-in web server for previewing the site.

Differences in server setup based on the environment, like MAMP including Apache or Nginx.

Renaming 'index.html' to 'index.php' to switch to dynamic PHP content.

Understanding the importance of the 'index' filename for web servers.

Writing PHP code within a PHP block to create dynamic content.

Using 'echo' to print a string in PHP and the importance of the semicolon.

Encouragement to practice inserting PHP echo statements in different parts of the document.

Building the first dynamic website using PHP and the importance of getting comfortable with PHP syntax.

Transcripts

play00:04

okay so our tools are now in place which

play00:06

means the next baby step is a little one

play00:09

let's just see if we can get something

play00:12

in the browser all right let's get going

play00:14

all right so i hope you're working along

play00:16

if so open up the terminal that you set

play00:19

up in the last episode and i'd like to

play00:21

create a directory for our first website

play00:24

but real quick and this will apply

play00:26

across the board for this series the way

play00:28

you go about creating a new site will

play00:30

slightly depend upon

play00:32

how you installed your environment how

play00:34

you installed php in your server so for

play00:37

example if you went with homebrew like i

play00:39

demonstrated in the last video

play00:41

you can store your website directories

play00:43

anywhere you want

play00:44

on the other hand if you went with

play00:45

something like map

play00:47

have a look at the documentation i think

play00:48

they will give you a specific directory

play00:51

where you should create your new website

play00:53

folders so again just look at the

play00:55

documentation for whatever

play00:57

installation path you chose and then

play00:59

follow that in my case i used homebrew

play01:02

so i can put my websites anywhere i want

play01:05

in that case i will go to my home

play01:06

directory and on the mac i can cd to

play01:09

that home directory by using tilde

play01:12

forward slash

play01:13

for windows i believe it'll be something

play01:15

like cd to sql in and then maybe users

play01:18

your name and desktop i think that's

play01:21

right okay let's make a directory for

play01:24

all of our websites i can use this

play01:26

command

play01:27

mkdir make directory and we'll call it

play01:30

websites

play01:32

whatever you want some people call it

play01:33

code it doesn't matter

play01:36

okay now i can cd to that directory

play01:39

change directory

play01:40

to websites

play01:41

okay so now yeah we can name this first

play01:44

project anything we want i will just

play01:46

call it demo so once again make

play01:48

directory called demo and we'll cd into

play01:51

that new folder

play01:52

okay i think we're ready to get started

play01:55

open up the editor that you chose in the

play01:56

last episode

play01:58

and then in my case i can choose open

play02:00

here but for you it might be file and

play02:02

open directory something like that

play02:05

okay and if i open my home directory

play02:06

sure enough i can see that websites

play02:08

folder at the top and demo is the one i

play02:10

want to open okay i'm excited so let me

play02:13

click up here on the top left and i only

play02:15

want to see my project files and we're

play02:18

all set so let's right click and choose

play02:20

new file

play02:22

and to begin i'm going to call it

play02:24

index.html

play02:26

not php this is a simple static html

play02:29

file now most editors will include any

play02:32

number of snippets for common tasks in

play02:34

my case i want some basic boilerplate

play02:36

for an html file and this is what it

play02:39

gives me but let's keep it super simple

play02:41

i'm going to get rid of these two meta

play02:42

tags and keep it like this

play02:45

all right now actually a quick note if

play02:47

what i've just done here in this html

play02:49

looks foreign to you you may not be

play02:52

ready for this particular series i would

play02:54

hit the pause button and research

play02:56

something like html for dummies go

play02:58

through that get a little comfortable

play03:00

learn a bit of css and then you're ready

play03:02

for this course okay

play03:04

so yeah let's just say h1 a heading one

play03:07

and we'll say hello world

play03:09

every series has to begin with a simple

play03:11

hello world

play03:12

okay so now i want to view it in the

play03:14

browser so it may turn out that your

play03:16

editor has some kind of functionality

play03:18

for viewing the results inline like you

play03:20

see here but why don't we just load it

play03:22

directly in a browser so that we're all

play03:24

on the same page

play03:25

so to do that i will return to the

play03:27

terminal and let's run php dash h this

play03:30

brings up the help for php and have a

play03:34

look right here bash s runs with a

play03:36

built-in web server it sounds like this

play03:38

is what we want but real quick once

play03:40

again a discrepancy if you went with

play03:42

something like mamp they will include a

play03:44

server for you you probably have apache

play03:46

or nginx and they will have instructions

play03:48

for how to preview your website in the

play03:50

browser otherwise you'll need to boot

play03:52

one up yourself and that's what i need

play03:54

to do here

play03:55

so i can say php dash s and let's go

play03:58

with localhost and a port how about 8888

play04:04

all right we have a development server

play04:06

running and in fact i should be able to

play04:08

command click on this url to access it

play04:11

otherwise just copy it and paste it into

play04:13

a browser all right congratulations

play04:15

you've now booted up a server and we're

play04:17

displaying a static piece of html great

play04:21

but i want you to notice that it

play04:22

automatically worked and part of that is

play04:25

because i named the file index so for

play04:27

example if i were to rename this file

play04:30

and on phpstorm i can go to refactor

play04:33

rename or there's always a shortcut and

play04:35

yeah if i changed it to anything else

play04:38

well keep in mind as soon as i return to

play04:40

the browser and refresh it won't be able

play04:42

to find that so yeah you'll often find

play04:44

that index is an important file name

play04:48

okay but now i want this to be dynamic

play04:51

i'm assuming you already have a little

play04:52

experience building a static website at

play04:54

least the basics to instead make it

play04:57

dynamic i'm going to switch to php

play05:00

so once again i will rename this file

play05:02

to

play05:04

index.php

play05:05

now again notice if i don't change

play05:07

anything at all because i'm running

play05:09

php's built-in web server it'll still

play05:12

work just like the previous example but

play05:14

now we have the full power of php at our

play05:18

fingertips so let's have a look here

play05:20

let's clear up this h1 tag and let's

play05:23

open up our first php block

play05:26

we do that by typing less than sign

play05:28

question mark php and then a space and

play05:31

then another question mark and a greater

play05:32

than sign

play05:34

and i'll warn you it's really confusing

play05:35

the first time you do it maybe the first

play05:37

10 times you do it you'll have to look

play05:39

at the keyboard to make sure you're

play05:40

hitting the right keys but i promise

play05:42

after a while you won't even think about

play05:44

it okay so now we're basically

play05:46

designating that whatever occurs between

play05:50

or inside this php block should be

play05:52

treated as not html but php

play05:56

so for example if we just did something

play05:57

like this and i once again wrote hello

play05:59

world this is not going to work and in

play06:02

fact if you have a good editor it'll let

play06:04

you know hmm there's something going on

play06:06

here

play06:07

switch back to the browser reload it and

play06:09

we get a parse error so notice we're

play06:13

trying to interpret this php but it has

play06:15

no clue what this is because it's not

play06:17

html we have to write using the php

play06:21

language

play06:22

so i'll show you the easiest way to

play06:24

simply print a string we can do that by

play06:27

saying echo and then within quotes your

play06:30

string hello world and then at the very

play06:33

end add a semicolon and in fact i just

play06:36

used the word string which can be a

play06:38

little confusing at first whenever you

play06:40

hear that word all i mean is basic text

play06:43

a string of characters that mean exactly

play06:46

what you read here they are not special

play06:49

symbols or identifiers that the language

play06:51

will understand it's literally text

play06:55

okay all right so think about what's

play06:56

going on here when the page loads we

play06:59

display a heading level 1 tag and as its

play07:02

text content we open up a php block and

play07:05

we echo out the string hello world and

play07:08

yeah i know echo can be a little

play07:10

confusing think of it just like print

play07:12

this text onto the page and in fact

play07:14

print is something that we could use

play07:16

here and it's mostly the same as echo

play07:19

there's some slight differences but but

play07:21

still

play07:22

stick with echo most people do

play07:24

all right so if we switch back give it a

play07:26

refresh we get the exact same thing as

play07:29

our static version but again i hope

play07:31

you're excited by definition we are now

play07:34

building our first dynamic website that

play07:36

uses php and we'll keep taking this

play07:39

further and further

play07:40

so to finish up today's learning i want

play07:43

you to play around with echoing

play07:45

different strings hello universe um

play07:48

hello town whatever you want practice

play07:51

inserting that text in other areas of

play07:53

the document maybe add a paragraph tag

play07:56

where you echo out some gibberish string

play08:00

the entire point is to become

play08:01

comfortable with writing this awkward

play08:04

sequence of characters you'll get it

Rate This

5.0 / 5 (0 votes)

Related Tags
Web DevelopmentPHP BasicsHTML CodingServer SetupLocalhost TestingPHP EchoDynamic ContentWeb ServerPHP SyntaxHello World