Download presentation
Presentation is loading. Please wait.
1
PHP variables
2
A symbol for something that changes
PHP variables are A symbol for something that changes What is your name? What day is it?
3
A symbol for something that changes
PHP variables are A symbol for something that changes Often the info is stored in a database or submitted by a form online What is your name? What day is it?
4
Call upon PHP variables with a $ My name is $name Today is $day
5
PHP variables always end line with semi-colon $name=Sarada;
6
variable on the left, value on the right
PHP variables assign a value variable on the left, value on the right $name=Sarada;
7
strings go in quotes (single or double) $name=Sarada
PHP variables strings go in quotes (single or double) $name=Sarada $description=‘blah di blah blah blah’;
8
name it whatever you want, but…
PHP variables name it whatever you want, but… always begin with $ first character after $ can not be a number no spaces (use underscore or camelcase) case-sensitive text (called strings) in quotes numbers need no quotes
9
PHP variables examples $word $Word $greenThing $green_thing $word4 do not use reserved words like: $this $function $include etc. list of reserved words:
10
once a value is assigned to a variable
PHP variables once a value is assigned to a variable you can display it with echo command <?php echo $name; ?>
11
log-in to your student web hosting and choose file manager
12
choose “public_html” folder
13
choose new file
14
name it variables.php
15
highlight variables.php click on code editor
16
write this: <?php echo “Hello World!”; ?>
17
open up your web page/variables.php
18
write this: <?php echo “Hello World!”; echo “<br />”;
print “Well hello to you too”; ?>
19
refresh your page
20
now back to the code editor, write this:
<!doctype html> <html> <head> <title>variables</title> </head> <body> <h1>This is a heading</h1> <p>this is a paragraph with words and stuff</p> </body> </html>
21
for this we will link to a separate CSS sheet to make room.
save CSS with extension .css in the site folder Inside the head tags, link to the CSS like this: <link rel="stylesheet" type="text/css" href="name.css">
22
<title>variables</title> </head> <body>
Normally variables come from a database, but for now we will hard code them into the page itself. Write this: <!doctype html> <html> <head> <title>variables</title> </head> <body> <?php $name = LaGuardia; ?> <h1>This is a heading</h1> <p>this is a paragraph with words and stuff</p> </body> </html>
23
<title>variables</title> </head> <body>
change this: <!doctype html> <html> <head> <title>variables</title> </head> <body> <?php $name = LaGuardia; ?> <h1>This is <?php echo $name;?></h1> <p>this is a paragraph with words and stuff</p> </body> </html>
24
mad libs we choose 6 verbs, 6 nouns, 6 adjectives, 1 pronoun you write these variables on the page in-between <?php ?> you choose a text from Blackboard. you fill in the appropriate echo command where specified we read them out loud
25
strings otherwise known as text defined inside quotes
26
create new file save as strings.php
27
<title>strings</title> </head> <body>
Write this, or copy and paste this from last page (change title): <!doctype html> <html> <head> <title>strings</title> </head> <body> <h1>This is a heading</h1> <p>this is a paragraph with words and stuff</p> </body> </html>
28
<title>strings</title> </head> <body> <?php
create a simple php string <!doctype html> <html> <head> <title>strings</title> </head> <body> <?php echo “Hello World <br />”; echo ‘Hello World again’; ?> <h1>This is a heading</h1> <p>this is a paragraph with words and stuff</p> </body> </html>
29
open up your web page/strings.php
30
combine strings together (concatnate) with a ‘
combine strings together (concatnate) with a ‘.’, and create empty space with “ ”: <!doctype html> <html> <head> <title>strings</title> </head> <body> <?php echo “Hello World <br />”; echo ‘Hello World again’; $expression = “Hello”; $subject = “World”; $greeting = $expression . “ ” . $subject; echo $greeting; ?> <h1>This is a heading</h1> <p>this is a paragraph with words and stuff</p> </body> </html>
31
place variables inside of strings (only works with double quotes)
<!doctype html> <html> <head> <title>strings</title> </head> <body> <?php echo “Hello World <br />”; echo ‘Hello World again’; $expression = “Hello”; $subject = “World”; $greeting = $expression . “ ” . $subject; echo $greeting; ?> <br /> echo “$greeting one more time”;
32
functions
33
using functions to manipulate string values
argument to make a string uppercase: strtoupper functions use ( ), don’t forget them <?php $name = LaGuardia; ?> <p>we are in a class at <?php echo $name;?></p> <p>we are in a class at<?php echo strtoupper ($name);?></p> <p>we are in a class at<?php echo strtolower ($name);?></p>
34
make one of the nouns uppercase using string to upper argument
strtoupper
35
capture the value by making the argument a variable itself
original value <p><?php echo $name;?></p> changed value <p><?php echo strtoupper ($name);?></p> make a variable from changed value <p><?php $changed strtoupper ($name);?></p> <p><?php $changed;?>
36
$noun1 = Manhattan Island;
$noun2 = Pineapple; $noun3 = Pizza; $noun4 = Tiger; $noun5 = Apple; $noun6 = Pen; Verb Dream Punch Hear Learn Run Eat Adjective Blindly Kind Pessimistic Mean Fuzzy Loud Pronoun Doctor President Veterinarian
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.