DIG1108C Lesson 2 Fall 2014
-
Upload
david-laietta -
Category
Documents
-
view
55 -
download
2
description
Transcript of DIG1108C Lesson 2 Fall 2014
Intro to Server-Side Programming
Week Two
PHP As A Language
• PHP can be treated as a distinct language with it’s own rules
• It has it’s own vocabulary - words that are familiar to the language
• PHP has distinct grammatical rules
• Syntax - rules to determine the composition of sentences
• Semantics - the meaning and significance of words
• Structure - the relationship between words
Grammar and Vocaublary
• Reserved Words and Keywords: if, else, ifelse; for, foreach, while, do; and, or, xor; public, private, protected
• Expressions: semicolon (;), curly braces ({...})
• Arithmetic: addition (+), subtraction (-), multiplication(*)
• Comparisons: equality (==), inequalities (!=), ranges (<=)
• Special cases: dollar($), braces ([...]), operators (&, |, <<)
• User & Built-in Constants: PHP_VERSION, E_ERROR, E_WARNING, E_NOTICE, MY_CONSTANT
• User & Built-in Functions: shuffle(), printf(), strlen()
The PHP Manual - http://php.net/manual/en/• In your “assignments” workspace, create a file called “assignment-2.1.md” to work on your
answers to the following questions:
• Find at least three different methods to get a random value from an array with a built-in function (Look in Array Functions)
• What does the built-in md5() function do? What is the default return value? What built-in functions work similarly?
• What is another name for “anonymous functions” in PHP and where are they in the manual? When were they added and what was the last feature added?
• What is the default value of the “memory_limit” setting in PHP? What value would I set to it in order to allocate ALL memory to PHP? How could I set that value and where would I find that in the manual?
Data Types - Literals
• Literals are also known as Primitive Values
• Representative of fixed values, such as numbers or text
• Four scalar types: boolean, integer, float (double) & string
• Two compound types: array & object
• Three special types: resource, NULL & callable
• What makes 1 different from 1.0?
Working With Variables
• Variables are storage locations with identifiers that contain values
• Variables are places to store your literals. Think of a bucket.
$a_variable // This is a bucket to hold literals!
$a_variable = “some value” // Now the bucket has stuff in it
$a_variable = “something else” // The bucket remains, but a new string is put in it
• Variables in PHP are always preceded with a dollar sign ($)
• Variables are not very useful empty, so they are usually followed with an assignment operator
Assignment Operators
• An operator accepts one or more literals or variables, performs an operation on them and returns the results, like a calculator.
$total = 1 // total is defined as 1
$total = $total + 1 // total is now 2
$total += 2 // total is now 4 (old total plus 2)
• Orders of operation and precedence still apply here!
Basic “Phrases”
• Variables, basic expressions and comments:
$david = ‘awesome’; // not entirely true
• Basic assignment and arithmetic operators:
$counter = 1; $counter = 2;
$counter = 1 + 1;
$counter += 2;
• Getting feedback from PHP:
echo $counter;
var_dump($counter); var_export($counter);
Assignment Two
Pulling Files From Git (cont)
• Open the project that you forked earlier or fork one now
• Grab around 50 lines of PHP from that project that show examples of literals, variables, function calls and other colored (important) things
• If there is something that you don’t recognize, note it for later
• The goal is to practice “reading” PHP grammar for now