php basics

38
PHP Basics Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com [email protected] @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program

Upload: jamshid-hashimi

Post on 10-May-2015

10.555 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Php basics

PHP Basics

Jamshid HashimiTrainer, Cresco Solution

http://www.jamshidhashimi.com [email protected] @jamshidhashimi ajamshidhashimi

Afghanistan Workforce Development Program

Page 2: Php basics

AGENDA

• About PHP & MySQL• Advantage of using PHP for Web Development• PHP Installation, PHP Syntax & PHP Variable• PHP Strings• PHP Operators• Conditional Statements– if (...else) Statement– Switch Statements

• Exercise!

Page 3: Php basics

PHP: Introduction

• PHP is a programming language for building dynamic, interactive Web sites. As a general rule, PHP programs run on a Web server, and serve Web pages to visitors on request. One of the key features of PHP is that you can embed PHP code within HTML Web pages, making it very easy for you to create dynamic content quickly.

Page 4: Php basics

PHP: Introduction

• PHP stands for PHP: Hypertext Preprocessor, which gives you a good idea of its core purpose: to process information and produce hypertext (HTML) as a result.

• PHP is a server-side scripting language, which means that PHP scripts, or programs, usually run on a Web server. (A good example of a client-side scripting language is JavaScript, which commonly runs within a Web browser.)

Page 5: Php basics

PHP: The Process

• A visitor requests a Web page by clicking a link, or typing the page’s URL into the browser’s address bar. The visitor might also send data to the Web server at the same time, either using a form embedded in a Web page, or via AJAX (Asynchronous JavaScript And XML).

• The Web server recognizes that the requested URL is a PHP script, and instructs the PHP engine to process and run the script.

• The script runs, and when it’s finished it usually sends an HTML page to the Web browser, which the visitor then sees on their screen.

Page 6: Php basics

PHP• PHP: Hypertext Preprocessor• Appeared in: 1995; 18 years ago• Designed by: Rasmus Lerdorf• Developer: The PHP Group• Stable release: 5.4.15 (May 9, 2013; 25 days ago)• Typing discipline: Dynamic, weak• Influenced by: Perl, C, C++, Java, Tcl• Implementation language: C• OS: Cross-platform• License: PHP License• Usual filename extensions .php, .phtml, .php4 .php3, .php5, .phps

Page 7: Php basics

Why PHP?

• Easy to learn• Familiarity with syntax• Free of cost• Efficiency in performance• A helpful PHP community• Cross Platform

Page 8: Php basics

MySQL• MySQL is the most popular database system used with PHP.• MySQL is a database system used on the web• MySQL is a database system that runs on a server• MySQL is ideal for both small and large applications• MySQL is very fast, reliable, and easy to use• MySQL supports standard SQL• MySQL compiles on a number of platforms• MySQL is free to download and use• MySQL is developed, distributed, and supported by Oracle

Corporation• MySQL is named after co-founder Monty Widenius's daughter: My

Page 9: Php basics

PHP Editor + W(M)AMP Installation

Page 10: Php basics

PHP Syntax

• Basic PHP Syntax<?php// PHP code goes here?>

<!DOCTYPE html><html><body><h1>My first PHP page</h1><?phpecho "Hello World!";?></body></html>

Page 11: Php basics

PHP Variables

• Variables are a fundamental part of any programming language. A variable is simply a container that holds a certain value.

echo 2 + 2;

echo 5 + 6;

echo $x + $y;

Page 12: Php basics

PHP Variables

• A variable starts with the $ sign, followed by the name of the variable

• A variable name must begin with a letter or the underscore character

• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

• A variable name should not contain spaces• Variable names are case sensitive ($y and $Y are

two different variables)

Page 13: Php basics

Strings: Examples

Valid examples of strings:

$string1 = "This is a string in double quotes";$str = 'This is a somewhat longer, singly quoted string';$string2 = ""; // a string with zero characters

Page 14: Php basics

Strings: Examples

Single quote strings vs. Double quote strings:

$variable = "name";$text = "My $variable will print!";echo $text;> My name will print!$text = 'My $variable will not print!';echo $text;> My $variable will not print!

Page 15: Php basics

Strings: Concatenation(.)

• String concatenation is the string manipulation method when you join 2 or more strings together.

$str1 = ”AWD Training";$str2 = "-Cresco Solutions";echo $str1.$str2;> AWD Training-Cresco Solutions

Page 16: Php basics

Strings: strlen()

• Returns the length of the string

$str = "Kabul is the capital of Afghanistan";echo strlen($str);> 35

Page 17: Php basics

Strings: explode()

• explode — Split a string by string

$str = "This is a somewhat longer, singly quoted string";$new_str = explode(" ",$str);print_r($new_str);>Array ( [0] => This [1] => is [2] => a [3] => somewhat [4] => longer, [5] => singly [6] => quoted [7] => string )

Page 18: Php basics

Strings: strpos()

• strpos — Find the position of the first occurrence of a substring in a string

$str = "Kabul is the capital of Afghanistan";$position = strpos($str,"capital");echo $position;> 13

Page 19: Php basics

Strings: strtoupper()

• strtoupper — Make a string uppercase

$str = "Kabul is the capital of Afghanistan";echo strtoupper($str);> KABUL IS THE CAPITAL OF AFGHANISTAN

Page 20: Php basics

Strings: strtolower()

• strtolower — Make a string lowercase

$str = "Kabul is the capital of Afghanistan";echo strtolower($str);

> kabul is the capital of afghanistan

Page 21: Php basics

Strings: ucfirst()

• ucfirst — Make a string's first character uppercase

$str = "afghanistan";echo ucfirst($str);> Afghanistan

Page 22: Php basics

Strings: lcfirst()

• lcfirst — Make a string's first character lowercase

$str = "Afghanistan";echo lcfirst($str);> afghanistan

Page 23: Php basics

Strings: trim()

• trim — Strip whitespace (or other characters) from the beginning and end of a string

$str = ” Afghanistan is the heart of Asia";var_dump($str);>string(36) “Afghanistan is the heart of Asia"

$str = ” Afghanistan is the heart of Asia";var_dump(trim($str));> string(32) "Afghanistan is the heart of Asia”

Page 24: Php basics

Strings: str_replace()

• str_replace — Replace all occurrences of the search string with the replacement string

$str = "Balkh is the capital of Afghanistan";echo str_replace("Balkh","Kabul",$str);> Kabul is the capital of Afghanistan

Page 25: Php basics

Strings: substr()

• substr — Return part of a string

$str = "Afghanistan is the heart of Asia";echo substr($str,12);> is the heart of Asia

$str = "Afghanistan is the heart of Asia";echo substr($str,12,12);> is the heart

$str = "Afghanistan is the heart of Asia";echo substr($str,-4);> Asia

Page 26: Php basics

Operators

• In all programming languages, operators are used to manipulate or perform operations on variables and values. – PHP Arithmetic Operators– PHP Assignment Operators– PHP Incrementing/Decrementing Operators– PHP Comparison Operators– PHP Logical Operators

Page 27: Php basics

Operators: Arithmetic

Page 28: Php basics

Operators: Assignment

Page 29: Php basics

Operators: Increment & Decrement

Page 30: Php basics

Operators: Comparison

Page 31: Php basics

Operators: Logical

Page 32: Php basics

Conditional Statements

if (condition){ code to be executed if condition is true;}

if (condition){

code to be executed if condition is true;}else{

code to be executed if condition is false;}

Page 33: Php basics

Conditional Statements

if (condition){

code to be executed if condition is true;}else if (condition){

code to be executed if condition is true;}else{

code to be executed if condition is false;}

Page 34: Php basics

Conditional Statements

switch (n){case label1: code to be executed if n=label1; break;case label2: code to be executed if n=label2; break;default: code to be executed if n is different from both label1 and label2;}

Page 35: Php basics

Exercise!

• Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise.

• Write a program called CheckOddEven which prints "Odd Number" if the int variable “number” is odd, or “Even Number” otherwise.

Page 36: Php basics

Exercise!

• Write a program which add, subtract, divide and multiply two number and show the result on the screen.

• Write a program to find the biggest positive number among a,b,c,d numbers

• Write a program which returns the average of given 3 number

Page 37: Php basics

Exercise!

• Write a program which categorize people according to their work experience:

0: Fresh Graduate1-3: Mid-Level4-8: Senior Level9-30: Superman• Write a program which outputs the quarter

(Q1,Q2,Q3,Q4) of the year a given date is in. – e.g. 17 June 2013 output must be: Q2 2013

Page 38: Php basics

QUESTIONS?