SlideShare ist ein Scribd-Unternehmen logo
1 von 38
PHP Basics
Jamshid Hashimi
Trainer, Cresco Solution
http://www.jamshidhashimi.com
jamshid@netlinks.af
@jamshidhashimi
ajamshidhashimi
Afghanistan Workforce
Development Program
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!
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.
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.)
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.
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
Why PHP?
• Easy to learn
• Familiarity with syntax
• Free of cost
• Efficiency in performance
• A helpful PHP community
• Cross Platform
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
PHP Editor + W(M)AMP Installation
PHP Syntax
• Basic PHP Syntax
<?php
// PHP code goes here
?>
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
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;
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)
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
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!
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
Strings: strlen()
• Returns the length of the string
$str = "Kabul is the capital of
Afghanistan";
echo strlen($str);
> 35
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 )
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
Strings: strtoupper()
• strtoupper — Make a string uppercase
$str = "Kabul is the capital of Afghanistan";
echo strtoupper($str);
> KABUL IS THE CAPITAL OF AFGHANISTAN
Strings: strtolower()
• strtolower — Make a string lowercase
$str = "Kabul is the capital of
Afghanistan";
echo strtolower($str);
> kabul is the capital of afghanistan
Strings: ucfirst()
• ucfirst — Make a string's first character
uppercase
$str = "afghanistan";
echo ucfirst($str);
> Afghanistan
Strings: lcfirst()
• lcfirst — Make a string's first character
lowercase
$str = "Afghanistan";
echo lcfirst($str);
> afghanistan
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”
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
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
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
Operators: Arithmetic
Operators: Assignment
Operators: Increment & Decrement
Operators: Comparison
Operators: Logical
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;
}
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;
}
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;
}
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.
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
Exercise!
• Write a program which categorize people
according to their work experience:
0: Fresh Graduate
1-3: Mid-Level
4-8: Senior Level
9-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
QUESTIONS?

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

PHP variables
PHP  variablesPHP  variables
PHP variables
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
php
phpphp
php
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
PHP slides
PHP slidesPHP slides
PHP slides
 
Php string function
Php string function Php string function
Php string function
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 

Andere mochten auch (8)

Advantages of Choosing PHP Web Development
Advantages of Choosing PHP Web DevelopmentAdvantages of Choosing PHP Web Development
Advantages of Choosing PHP Web Development
 
Php hypertext pre-processor
Php   hypertext pre-processorPhp   hypertext pre-processor
Php hypertext pre-processor
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Linux Introduction (Commands)
Linux Introduction (Commands)Linux Introduction (Commands)
Linux Introduction (Commands)
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Ähnlich wie Php basics

Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Muhamad Al Imran
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
Muhamad Al Imran
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
HambaAbebe2
 

Ähnlich wie Php basics (20)

Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php Basics
Php BasicsPhp Basics
Php Basics
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Prersentation
PrersentationPrersentation
Prersentation
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 

Mehr von Jamshid Hashimi

CodeIgniter Helper Functions
CodeIgniter Helper FunctionsCodeIgniter Helper Functions
CodeIgniter Helper Functions
Jamshid Hashimi
 
Managing Applications in CodeIgniter
Managing Applications in CodeIgniterManaging Applications in CodeIgniter
Managing Applications in CodeIgniter
Jamshid Hashimi
 
PHP Frameworks & Introduction to CodeIgniter
PHP Frameworks & Introduction to CodeIgniterPHP Frameworks & Introduction to CodeIgniter
PHP Frameworks & Introduction to CodeIgniter
Jamshid Hashimi
 
Exception & Database
Exception & DatabaseException & Database
Exception & Database
Jamshid Hashimi
 
MySQL Record Operations
MySQL Record OperationsMySQL Record Operations
MySQL Record Operations
Jamshid Hashimi
 

Mehr von Jamshid Hashimi (20)

Week 2: Getting Your Hands Dirty – Part 2
Week 2: Getting Your Hands Dirty – Part 2Week 2: Getting Your Hands Dirty – Part 2
Week 2: Getting Your Hands Dirty – Part 2
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1
 
Introduction to C# - Week 0
Introduction to C# - Week 0Introduction to C# - Week 0
Introduction to C# - Week 0
 
RIST - Research Institute for Science and Technology
RIST - Research Institute for Science and TechnologyRIST - Research Institute for Science and Technology
RIST - Research Institute for Science and Technology
 
How Coding Can Make Your Life Better
How Coding Can Make Your Life BetterHow Coding Can Make Your Life Better
How Coding Can Make Your Life Better
 
Mobile Vision
Mobile VisionMobile Vision
Mobile Vision
 
Tips for Writing Better Code
Tips for Writing Better CodeTips for Writing Better Code
Tips for Writing Better Code
 
Launch Your Local Blog & Social Media Integration
Launch Your Local Blog & Social Media IntegrationLaunch Your Local Blog & Social Media Integration
Launch Your Local Blog & Social Media Integration
 
Customizing Your Blog 2
Customizing Your Blog 2Customizing Your Blog 2
Customizing Your Blog 2
 
Customizing Your Blog 1
Customizing Your Blog 1Customizing Your Blog 1
Customizing Your Blog 1
 
Introduction to Blogging
Introduction to BloggingIntroduction to Blogging
Introduction to Blogging
 
Introduction to Wordpress
Introduction to WordpressIntroduction to Wordpress
Introduction to Wordpress
 
CodeIgniter Helper Functions
CodeIgniter Helper FunctionsCodeIgniter Helper Functions
CodeIgniter Helper Functions
 
CodeIgniter Class Reference
CodeIgniter Class ReferenceCodeIgniter Class Reference
CodeIgniter Class Reference
 
Managing Applications in CodeIgniter
Managing Applications in CodeIgniterManaging Applications in CodeIgniter
Managing Applications in CodeIgniter
 
CodeIgniter Practice
CodeIgniter PracticeCodeIgniter Practice
CodeIgniter Practice
 
CodeIgniter & MVC
CodeIgniter & MVCCodeIgniter & MVC
CodeIgniter & MVC
 
PHP Frameworks & Introduction to CodeIgniter
PHP Frameworks & Introduction to CodeIgniterPHP Frameworks & Introduction to CodeIgniter
PHP Frameworks & Introduction to CodeIgniter
 
Exception & Database
Exception & DatabaseException & Database
Exception & Database
 
MySQL Record Operations
MySQL Record OperationsMySQL Record Operations
MySQL Record Operations
 

KĂźrzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

KĂźrzlich hochgeladen (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Php basics

  • 1. PHP Basics Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com jamshid@netlinks.af @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program
  • 2. 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!
  • 3. 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.
  • 4. 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.)
  • 5. 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.
  • 6. 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
  • 7. Why PHP? • Easy to learn • Familiarity with syntax • Free of cost • Efficiency in performance • A helpful PHP community • Cross Platform
  • 8. 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
  • 9. PHP Editor + W(M)AMP Installation
  • 10. PHP Syntax • Basic PHP Syntax <?php // PHP code goes here ?> <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
  • 11. 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;
  • 12. 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)
  • 13. 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
  • 14. 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!
  • 15. 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
  • 16. Strings: strlen() • Returns the length of the string $str = "Kabul is the capital of Afghanistan"; echo strlen($str); > 35
  • 17. 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 )
  • 18. 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
  • 19. Strings: strtoupper() • strtoupper — Make a string uppercase $str = "Kabul is the capital of Afghanistan"; echo strtoupper($str); > KABUL IS THE CAPITAL OF AFGHANISTAN
  • 20. Strings: strtolower() • strtolower — Make a string lowercase $str = "Kabul is the capital of Afghanistan"; echo strtolower($str); > kabul is the capital of afghanistan
  • 21. Strings: ucfirst() • ucfirst — Make a string's first character uppercase $str = "afghanistan"; echo ucfirst($str); > Afghanistan
  • 22. Strings: lcfirst() • lcfirst — Make a string's first character lowercase $str = "Afghanistan"; echo lcfirst($str); > afghanistan
  • 23. 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”
  • 24. 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
  • 25. 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
  • 26. 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
  • 32. 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; }
  • 33. 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; }
  • 34. 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; }
  • 35. 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.
  • 36. 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
  • 37. Exercise! • Write a program which categorize people according to their work experience: 0: Fresh Graduate 1-3: Mid-Level 4-8: Senior Level 9-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