declare

PHP Tutorial (1) Strings, Variables, Echo, Arrays



Sharing buttons:

hey guys this is Jesse with

ENCOM this is a tutorial about the

basics of PHP PHP is a loosely typed

language and I'm going to assume you

already have your server installed so

that you can load PHP on your hard drive

if you don't you probably want to look

on google for something called lamp

if you're on Windows ma'am they've drawn

Macintosh or lamp if you're on Linux and

that'll help you get started the first

thing you want to do is go into your

working server directory and just create

an index file and that's where I'm here

every page starts with PHP with a little

question mark and to end it you just do

that most of the time I don't end it to

create let's just say to echo something

echo hello my name is Jesse okay I'm

using single quotes here and I'll

explain the difference between single

and double quotes in a minute so if we

load this hello my name is Jesse okay if

you use double quotes it'll still print

the same thing but let's say we want to

make a variable and all variables start

with a dollar sign and then let's just

call it name and name equals Jesse okay

now if I want to put this variable

inside of this string right here I can

type name and here's where you'll see

the problem with the single quotes see

it prints it literally but if you use

the double quotes it will actually parse

the value okay hello my name is Jesse so

most of the time you want to use single

quotes unless you're

wanting to parse a value another

alternative you could do is let's

pretend these are back to single quotes

it's called concoct concoction ation I

don't know how to say it

well you just add a period it's kind of

like adding to a string hello my name is

Jesse C we forgot a space after the

string here is that a space there there

it is and if you want to add on a little

bit more you just add another period and

I like pizza okay so working with

variables it's a pretty basic you never

want to start one with a number this is

illegal you can start them with

underscores you can't start them with

dashes so just try to keep it usually

either with an underscore or a letter to

start it with if you want to do a digit

you don't have to put the quotes around

it and this is a literal integer so age

is 34 I'm actually 25 and I am dollar

age years old hello my name is Jesse and

I like pizza and I'm 25 years old sound

like a real cool guy huh other things

you can do I'm guessing you're gonna

pick up all this variable stuff pretty

quick let's say we want name two to

equal name

you just type name two equals name I'm

going to erase all this so echo name two

and if you guessed right name two is

going to help put Jesse okay

now if we want a three variables to

equal that you can just do the same

thing name three equals that and you

could do echo name three easy peasy

there's also reference variables these

might be a little more confusing and

something you really don't need to worry

about off the bat but they're within a

and sign

so anyways there's a lot of variable

manipulation you can do it's not too

hard to pick up one of the things you'll

be working with a ton in PHP is called

arrays and to make an array you just

type in array like that usually we want

to give an array of variable so let's

just call it a names equals array okay

now arrays are going to take quite a bit

of practice to get used to but they're

one of the most useful things you can

understand and I'm just going to start

out with a simple array and these are

gonna be some names Jessie comma Joey

Jenny and Justine okay now if you want

to echo out names you're going to see it

says array an array has a key and a

value so even though this looks like

this is all it is really there's a

hidden key right here so this would

start at 0 1 2 and so on

so if I wanted to echo names in the very

first slot which is Jessie you use these

square brackets and you type 0 because

names arrays they always start with 0

and refresh when you get Jessie so it

might look confusing that justine looks

like it's slot number 4 but that would

actually be number 3 because you go 0 1

2 3 and there's just in you can also do

what are called associative arrays okay

and those are arrays identified by a

string like this because normally it

looks like this like I just showed you

now you can't have the same keys so

you're gonna have to count these if you

type them in manually same result

associative arrays they have a for this

slot here it's a string so let's call

this oldest boy or a youngest boy this

one's oldest boy this one's oldest girl

and youngest girl okay now if you echo

names 3 since this is an associative

array you're not going to get anything

so you would actually have to do names

youngest girl and that will give us

Justine okay you might say well why

would I want to use an array oh when

you're dealing with like a lot of data

and you can also sort arrays and they're

like little pockets to store chunks of

data and very useful probably one of the

more important things you need to learn

is to use arrays

if I put this back just to how it was

okay and let's just echo names too

and that should give us Jenny okay now

you can do things like sort names and

that'll give us Joey why because it

sorted it alphabetically and as you can

see Jenny was slot - did you see zero

one - since it's sorted alphabetically

Joey - oh comes after Jenny so it really

moved it to here okay there's a lot of

sorting things you can use like case or

and everything but before this tutorial

is over let's do something kind of fun

so that it feels like you're not just

learning about nothing let's make a form

so we end our PHP tags type form action

equals index dot PHP that's the name of

this page and method equals post and

your form tags and let's make input type

equals text name equals let's just call

it name and type name before that and

then let's make a submit button input

type equals submit value equals print

name okay

so we have a little form here you click

this it just refreshes the page right

now let's have it do a post request

so we'll say post and what do we want to

post we want to post whatever is in this

text field and we'll grab that by the

name right here and we call the name

coincidentally so post name okay that's

how we post data from a form and we can

just do echo post name test and there it

is it's printed right at the top easy

peasy

now if you go back to your page just

index dot PHP see there's a problem

because it's trying to post this right

when you load the page so a good way

around that is to change the URL right

here just add a little query string by

doing that you add a question mark and

let's just type in check okay and we do

if I didn't go over if statements but

just type along for now I guess if is

set and this is a request not a post

request check then we will echo the post

name so we refresh this you see you get

no error any more tests and there it is

and you'll see the URL changes with a

question mark check and this is fetched

with a get or request I usually use

request and I suppose that's good for

the first video

you