PHP Language Basics.

Slides:



Advertisements
Similar presentations
Introduction to PHP Dr. Charles Severance
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Introduction to the C# Programming Language for the VB Programmer.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
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 (
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
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.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
1 PHP Introduction Chapter 1. Syntax and language constructs.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
PHP using MySQL Database for Web Development (part II)
Chapter 1.2 Introduction to C++ Programming
CGS 3066: Web Programming and Design Spring 2017
Session 2 Basics of PHP.
>> Introduction to JavaScript
Chapter 2 Variables.
Working with Java.
CS 371 Web Application Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Introduction to PHP Dr. Charles Severance
Web Technologies PHP 5 Basic Language.
Introduction to PHP Dr. Charles Severance
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Variables and Arithmetic Operators in JavaScript
JavaScript Syntax and Semantics
Computing with C# and the .NET Framework
Web Programming– UFCFB Lecture 19-20
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
String Conversion and Type Juggling
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to Programming in Java
JavaScript an introduction.
Statements, Comments & Simple Arithmetic
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 1: Computer Systems
FMI-PLOVDIV Web Dynamic Applications
PHP.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
elementary programming
Focus of the Course Object-Oriented Software Development
Chapter 2: Introduction to C++.
PHP an introduction.
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

PHP Language Basics

PHP code starts with <?php and ends with ?> written to a .php file <?php // statements ?>

anywhere in an HTML content PHP code can be placed anywhere in an HTML content <?php // PHP can be placed here ?> <html><head> </head><body> <?php // or here... ?> </body></html> <?php // or even here ?>

To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println) echo “Printy”; echo(“Printy”); // also valid

You can specify multiple items to print by separating them with commas echo “First”, “second”, “third”; Firstsecondthird

There is a shorter way to echo a variable <span><?php echo $name; ?></span> // shorter <span><?=$name?></span>

To check the type and content of a variable, use the var_dump() function var_dump($a); // int(10)

We can easily put multiline strings into your program with a heredoc (here documents) $html = <<< EndOfQuote <html><head> </head><body> Hello World! </body></html> EndOfQuote;

The names of user-defined classes and functions, as well as built-in constructs and keywords are case-insensitive

Thus, these three lines are equivalent: echo(“hello, world”);

Null keywords are also case-insensitive $val = null; // same

Variables, on the other hand, are case-sensitive. That is, $name, $NAME, and $NaME are three different variables

to separate statements PHP uses semicolons to separate statements

echo “Hello, world”; myFunction(42, “O'Reilly”); $a = 1; $name = “Elphaba”; $b = $a / 25.0; if ($a == $b) { echo “Rhyme? And Reason?”; }

Whitespace doesn’t matter in a PHP program

For example, this statement: could just as well be written raisePrices($inventory, $inflation, $costOfLiving, $greed); could just as well be written with more whitespace: raisePrices ( $inventory , $inflation , $costOfLiving , $greed ) ; or with less whitespace: raisePrices($inventory,$inflation,$costOfLiving,$greed);

PHP provides several ways to include comments within your code (all of which are borrowed from existing languages such as C, C++, and the Unix shell)

Shell-style comments: # create an HTML form requesting # that the user confirm the action echo confirmationForm();

C++ comments: // create an HTML form requesting // that the user confirm the action echo confirmationForm();

C comments: /* create an HTML form requesting that the user confirm the action */ echo confirmationForm();

Variable names always begin with a dollar sign ($) and are case-sensitive

Here are some valid variable names: $bill $head_count $MaximumForce $I_HEART_PHP $_underscore $_int

Here are some illegal variable names: $not valid $| $3wa

another of a different type You can replace a variable’s value with another of a different type (known as type juggling) $what = “Fred”; $what = 35; $what = array(“Fred”, 35);

You can reference the value of a variable whose name is stored in another variable $foo = “bar”; $$foo = “baz”; // $bar = “baz”

What variable contains 100? $foo = ‘bar’; $$foo = ‘world’; $$$foo = 100;

constant cannot change. Only scalar values can be constants Once set, the value of a constant cannot change. Only scalar values (Booleans, integers, floats, and strings) can be constants

Constants are set using the define() function: define(‘PUBLISHER’, “O’Reilly”); echo PUBLISHER;

Update: PHP 7 allows us to define array constants define(‘NAME’, [‘One’,‘Two’,‘Three’]);

You cannot give a variable, function, class, or constant the same name as a keyword

__CLASS__ and endforeach __DIR__ array() endif __FILE__ as endswitch __FUNCTION__ break endwhile __LINE__ echo eval() __METHOD__ else exit() __NAMESPACE__ elseif extends __TRAIT__ empty() final __halt_compiler() enddeclare insteadof abstract endfor interface

isset() require_once default list() return die() namespace callable do new case for or catch foreach print class function private clone global protected const goto public continue if require declare implements

include var include_once while instanceof xor static switch throw trait try unset() use

In PHP, arithmetic operators are very similar to that in Java $b = 3; var_dump($a + $b); // 13 var_dump($a - $b); // 7 var_dump($a * $b); // 30 var_dump($a / $b); // 3.333333... var_dump($a % $b); // 1 var_dump($a ** $b); // 1000 var_dump(-$a); // -10

Assignment operators are also very similar

But be careful! $a = ‘1’; $b = 2; $c = $a + b; $d = $a . $b;

There are also incrementing and decrementing operators $b--;

Comparison operators are similar, too var_dump(2 < 3); // true var_dump(3 < 3); // false var_dump(3 <= 3); // true var_dump(4 <= 3); // false var_dump(2 > 3); // false var_dump(3 >= 3); // true var_dump(3 > 3); // false

And so are the logical operators var_dump(true && true); var_dump(true && false); var_dump(true || false); var_dump(false || false); var_dump(!false);

But there are the PHP 7’s new “spaceship” operator var_dump(1 <=> 5); var_dump(1 <=> 1); var_dump(5 <=> 2);

PHP 7 also introduces the new null coalesce operator $uname = isset($_GET['uname']) ? $_GET['uname'] : 'guest'; var_dump($uname); $uname = $_GET['uname'] ?? 'guest'; $_GET['uname'] = 'admin';

When comparing values, we need to be careful with type juggling var_dump($a == $b); var_dump($a === $b); var_dump($a != $b); var_dump($a !== $b); var_dump($a == $c); var_dump($a <> $c);

Strings are delimited by either single or double quotes ‘big dog’ “fat hog”

Variables are interpolated within double quotes, while within single quotes they are not

Interpolation is the process of replacing variable names in the string with the values of those variables

$name = “Guido”; echo “Hi, $name”; echo ‘Hi, $name’; Hi, Guido Hi, $name

The other way is to surround the variable being interpolated with curly braces echo “You are the $nth person”; echo “You are the {$n}th person”; You are the 12th person

What’s the output? $var = ‘world’; $$var = 100; echo ‘Hello $var<br>’; echo “Hello $var<br>”; echo ‘Hello $world<br>’; echo “Hello $world<br>”;

To test whether two strings are equal, use the == operator if ($a == $b) { echo “a and b are equal” }

Use the is_string() function to test whether a value is a string if (is_string($x)) { // $x is a string }

gets the length of a string The strlen() function gets the length of a string $s = ‘FILKOM UB’; echo strlen($s); // 9

The strtolower() function converts all characters in a string to lowercase $s = ‘FILKOM UB’; echo strtolower($s); // filkom ub

The strtoupper() function converts all characters in a string to uppercase $s = ‘filkom ub’; echo strtoupper($s); // FILKOM UB

The trim() function removes all blank spaces surrounding a string $s = ‘ FILKOM UB ’; echo trim($s); // FILKOM UB

To concatenate a string, use the dot operators $s1 = ‘FILKOM ’; $s2 = ‘UB’; $s3 = $s1 . $s2; // FILKOM UB

Use the is_bool() function to test whether a value is a Boolean or not if (is_bool($x)) { // $x is a Boolean }

Use unset() to remove a variable $name = “Fred”; unset($name); // throws a PHP notice

has been set to something, To see if a variable has been set to something, use isset() $s1 = isset($name); // $s1 is false $name = “Fred”; $s2 = isset($name); // $s2 is true

values all evaluate to false: In PHP, the following values all evaluate to false: The keyword false The integer 0 The floating-point value 0.0 The empty string (“”) and the string “0” An array with zero elements An object with no values or functions The NULL value

$val = “”; if ($val) { // this won’t be executed }

bye.