Introduction to PHP Dr. Charles Severance www.php-intro.com.

Slides:



Advertisements
Similar presentations
Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
Advertisements

PHP Functions / Modularity
JavaScript Functions, Objects, Array, and Control Structures Dr. Charles Severance
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Outline Java program structure Basic program elements
Expressions and Control Flow in PHP Dr. Charles Severance
Getting PHP Hosting Dr. Charles Severance. What We Need We want to be able to put up our application on the web so anyone can access our URL (and so we.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Intro to PHP – Page 1 of 43CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Intro to PHP Reading: Chapters 1.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
Chapter 2: Java Fundamentals
What is PHP? IDIA Fall 2014 Bridget M Blodgett.
PHP Arrays Dr. Charles Severance
Variables, Expressions, and Statements
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Strings, output, quotes and comments
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
PHP Functions Dr. Charles Severance
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
JavaScript Dr. Charles Severance
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Expressions and Control Flow in PHP. Expressions Expressions evaluate to a value. The value can be a string, number, boolean, etc... Expressions often.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
PHP Arrays Dr. Charles Severance
PHP Functions / Modularity Dr. Charles Severance
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
CGS 3066: Web Programming and Design Spring 2016 PHP.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Introduction to Dynamic Web Content
Chapter 1.2 Introduction to C++ Programming
PHP Arrays Dr. Charles Severance
Definition of the Programming Language CPRL
PHP Functions / Modularity
Chapter 1.2 Introduction to C++ Programming
Introduction to PHP Dr. Charles Severance
Introduction to PHP Dr. Charles Severance
HTTP Parameters and Arrays
Introduction to PHP Dr. Charles Severance
PHP Arrays Dr. Charles Severance
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Expressions and Control Flow in PHP
Intro to PHP & Variables
PHP Namespaces (in progress)
Introduction to Dynamic Web Content
Introduction to C++ Programming
An overview of Java, Data types and variables
Chapter 1: Computer Systems
FMI-PLOVDIV Web Dynamic Applications
Chap 2. Identifiers, Keywords, and Types
PHP Language Basics.
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Introduction to PHP Dr. Charles Severance

About the PHP Language Syntax is inspired by C Curly braces, semicolons, no signficant whitespace Syntax inspired by perl Dollar signs to start variable names, associative arrays Extends HTML to add segments of PHP within an HTML file.

Philosphy of PHP You are a responsible and intelligent programmer You know what you want to do Some flexibility in syntax is OK - style choices are OK Lets make this as convienent as possible Sometimes errors fail silently

Hello from Dr. Chuck's HTML Page <?php echo "Hi there.\n"; $answer = 6 * 7; echo "The answer is $answer, what "; echo "was the question again?\n"; ?> Yes another paragraph.

Hello from Dr. Chuck's HTML Page <?php echo "Hi there.\n"; $answer = 6 * 7; echo "The answer is $answer, what "; echo "was the question again?\n"; ?> Yes another paragraph.

PHP From the Command Line You can run PHP from the command line - the output simply comes out on the terminal It does not have to be part of a request-response cycle <?php echo("Hello World!"); echo("\n"); ?>

Key Words php abstract and array() as break case catch class clone const continue declare default do else elseif end declare endfor endforeach endif endswitch endwhile extends final for foreach function global goto if implements interface instanceof namespace new or private protected public static switch $this throw try use var while xor

Variable Names Start with a dollar sign ($) followed by a letter or underscore, followed by any number of letters, numbers, or underscores Case matters cs.php $abc = 12; $total = 0; $largest_so_far = 0; abc = 12; $2php = 0; $bad-punc = 0;

Variable Name Weirdness Things that look like variables but are missing a dollar sign can be confusing $x = 2; $y = x + 5; print $y; $x = 2; y = $x + 5; print $x; 5 Parse error

Expressions Completely normal like other languages ( + - / * ) More agressive implicit type conversion <?php $x = "15" + 27; echo($x); echo("\n"); ?> 42

Output echo is a language construct - can be treated like a function with one parameter. Without parenthesis, it accepts multiple parameters. print is a function - only one parameter but parenthesis are optional so it can look like a language construct <?php $x = "15" + 27; echo $x; echo("\n"); echo $x, "\n"; print $x; print "\n"; print($x); print("\n"); ?>

Conditional - if Logical operators ( == != = && || ! ) Curly braces <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> Hello World!

Whitespace does not matter <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?>

What Style do You Prefer? <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> Aesthetic s

Associative Arrays Like Python Dictonaries+Lists - but more powerful Can be key => value or simply indexed by numbers Ignore two-dimensional arrays for now...

Integer Indices <?php $stuff = array("Hi", "There"); echo $stuff[1], "\n"; ?> There

Integer Indices <?php $stuff = array(); $stuff[] = "Hello"; $stuff[] = "World"; echo $stuff[1], "\n"; ?> World

Integer Indices <?php $stuff = array(); $stuff[2] = "Hello"; $stuff[9] = "World"; echo $stuff[9], "\n"; ?> World

Key / Value <?php $stuff = array("name" => "Chuck", "course" => "SI664"); echo $stuff["course"], "\n"; ?> SI664

Dumping an Array The function print_r() dumps out PHP data - it is used mostly for debugging <?php $stuff = array("name" => "Chuck", "course" => "SI664"); print_r($stuff); ?> Array ( [name] => Chuck [course] => SI664 )

Dumping an Array The function print_r() dumps out PHP data - it is used mostly for debugging <?php $stuff = array(); $stuff[2] = "Hello"; $stuff[9] = "World"; print_r($stuff); ?> Array ( [2] => Chuck [9] => SI664 )

var_dump.vs. print_r <?php $stuff = array("name" => "Chuck", "course" => "SI664"); var_dump($stuff); ?> array(2) { ["name"]=> string(5) "Chuck" ["course"]=> string(5) "SI664" } print-r

var_dump() is more verbose <?php $thing = FALSE; echo("One\n"); print_r($thing); echo("Two\n"); var_dump($thing); ?> One Two bool(false) print-r

Looping Through an Array <?php $stuff = array("name" => "Chuck", "course" => "SI664"); foreach($stuff as $k => $v ) { echo "Key=",$k," Val=",$v,"\n"; } ?> Key=name Val=Chuck Key=course Val=SI664

Variable Name Weirdness Things that look like variables but are missing a dollar sign as an array index are unpredictable.... $x = 5; $y = array("x" => "Hello"); print $y[x]; Hello

Strings String literals can use single quotes or double quotes The backslash (\) is used as an "escape" character Strings can span multiple lines - the newline is part of the string In double-quoted strings variable values are expanded g.php

<?php echo 'this is a simple string'; echo 'You can also have embedded newlines in strings this way as it is okay to do'; // Outputs: Arnold once said: "I'll be back" echo 'Arnold once said: "I\'ll be back"'; // Outputs: This will not expand: \n a newline echo 'This will not expand: \n a newline'; // Outputs: Variables do not $expand $either echo 'Variables do not $expand $either'; ?> Single Quote

<?php echo "this is a simple string\n"; echo "You can also have embedded newlines in strings this way as it is okay to do"; // Outputs: This will expand: // a newline echo "This will expand: \na newline"; // Outputs: Variables do 12 $expand = 12; echo "Variables do $expand\n"; ?> Double Quote

syntax.comments.php <?php echo 'This is a test'; // This is a c++ style comment /* This is a multi line comment yet another line of comment */ echo 'This is yet another test'; echo 'One Final Test'; # This is a shell-style comment ?>

Summary This is a sprint through the language features of PHP

Acknowledgements / Contributions These slides are Copyright Charles R. Severance ( as part of and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Continue new Contributors and Translators here