Web Development with PHP

PHP is a programming language. It’s also called server scripting language or general-purpose scripting language. PHP stands for Hypertext Preprocessor. Especially PHP is popular for Web Development, we can easily show dynamical HTML output with PHP.

What is PHP

  • Full form of PHP – Hypertext Preprocessor
  • Server scripting language, powerful tool/language for making dynamic websites

Note: The main difference between Scripting and Programming Language is – “Scripting language need interpreter, but programming language need compiler.

PHP is a Scripting Language, specially for Web Development.

Difference between Scripting Language and Programming Language

Scripting LanguageProgramming Language
Need Interpreter (executing sets of instructions)Need Compiler (compile into machine code first)

All scripting languages are programming languages, but not all programming languages are scripting languages

Why PHP

  • Popular, full free, and efficient
  • Flexible
  • Open source general-purpose scripting language
  • Specially suited for web development and can be embedded into HTML.
  • Best Part: PHP is that it is extremely simple for a newcomer,

How much PHP popular

PHP popular sites

https://w3techs.com/technologies/details/pl-php

All sites in bangladesh
https://trends.builtwith.com/framework//country/Bangladesh


https://techjury.net/blog/php-usage-statistics/#:~:text=77%25%20of%20all%20live%20websites,are%20still%20for%20web%20development.

https://kinsta.com/blog/is-php-dead/

https://www.linkedin.com/pulse/php-dead-least-according-usage-statistics-turnkey-labs/

Popular websites which is using PHP

Microsoft.com
Facebook.com
Wikipedia.org
WordPress.org
Pinterest.com
Vimeo.com
WordPress.com
Mozilla.org
Digicert.com
Europa.eu

What can do PHP

  • Server-side scripting: CGI features
  • Command line scripting
  • Writing desktop applications

History of PHP

  • First PHP created by Rasmus Ledorf at 1994 (Made by (CGI) binaries written in the C programming language)
  • Reason: Tracking visits to his online resume, so he named it Personal Home Page Tools, short form PHP tools

Environment Setup & Editor Setup for PHP

  • Xampp
  • Code Editor – Visual Studio Code
  • Web Browser – Google Chrome

PHP Syntax

Create a php file, a php file extension must be php.

// Opening and Closing PHP tag

<?php
   //php code
?>

// Short echo tag

<?=  ?>

Note 1: Short echo tag can be enable/disable by php.ini file
// Opening and Closing PHP tag

<?php
   //php code
?>

// Short echo tag

<?=  ?>

Note 1: Short echo tag can be enable/disable by php.ini file
// Opening and Closing PHP tag

<?php
   //php code
?>

// Short echo tag

<?=  ?>

Note 1: Short echo tag can be enable/disable by php.ini file
Note 2: If your PHP files contain only PHP code, it's preferable to skip the PHP closing tag

Naming Convention PHP

Naming Convention PHP

Output in PHP

  • output show by the echo
  • output show by printf
  • decimal number can control by %05d
  • float by number can control by %.2f

Statement, echo, print, PHP and HTML

Statement: A line of code.
echo: output string and variable (can be used with or without parentheses)
print: output string and variable (can be used with or without parentheses)

Note: echo is faster compare to print.

PHP and HTML

  • PHP and HTML code can use in same file, ensure your file is a php file.
  • Live

Variable and Constant

  • Variable: Variable is a value, data which can change.
  • Constant: Constant is also one type variable, but this value can’t be change while program is running.

Variable Name can be define by different way:
Camel Case: $myName
Pascal Case: $MyName
Snake Case: $my_name

Variable and Constant – Naming Convention

  • Must be start with $ sign
  • Must be start with Alphabet or Underscore
  • Name can contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

Note: Variable name are Case Sensitive.

Magic Constant

Magic constant are some pre defined constant by PHP, it’s change dynamically his value based on where it’s using.

    // print the line no
    
    
    echo '__LINE__ This instruction line number ' . __LINE__ . "<br>";  



    // print the file directory including file name
    echo '__FILE__ This file directory  ' . __FILE__ . "<br>";  
   

     // print the directory
    echo '__DIR__ This file directory ' . __DIR__ . "<br>";  


    // print the function name

    function my_sujon(){
        echo '__FUNCTION__ This statement from function ' . __FUNCTION__ . "<br>";  
        
    }


    
    my_sujon();

Operators

Operator TypeOperators
Arithmetic OperatorsAddition (+)
Subtraction (-)
Multiplication ()
Division (/)
Modulus (%)
Exponentiation (**)
Assignment operators
Comparison operatorsEqual (==)
Identical (===)
Not equal (!=)
Not equal (<>)
Not identical (!==)
Greater than (>)
Less than (<)
Greater than or equal to (>=)
Less than or equal to (<=)
Spaceship (<=>)
Increment/Decrement operatorsPre-increment (++$i)
Post-increment ($i++)
Pre-decrement (–$x)
Post-decrement ($x–)
Logical operatorsAnd (and)
Or (or)
Xor (xor)
And (&&)
Or (||)
Not (!)
String operatorsConcatenation (.)
Concatenation assignment (.=)
Array operatorsUnion (.)
Equality (==)
Identity (===)
Inequality (!=)
Inequality (<>)
Non-identity (!==)
Conditional assignment operatorsTernary (?:)
Null coalescing (??)

PHP Data Types

There are mainly 8 datatypes in PHP.

  1. String
  2. Integer
  3. Float (floating point numbers – also called double)
  4. Boolean
  5. Array
  6. Object
  7. NULL
  8. Resource
Data TypesDescription
StringWhen a variable store character or text, it’s a String.
Example:
$x = “abc”;

Note: String can wrap with single quote or double quote.
IntegerInteger means the number, which can have a negative or positive value, but not a decimal point. Integer has a limit, it depends on the computer bit.
FloatFloat means the decimal point number.
BooleanBoolean use for conditional testing. True and False can be values of a boolean type variable. (Case Insensitive)
ArrayArray is one kind of date type, which can store multiple data at a time.
Object Object use for OOP
NULLNull mean where you have assign nothing, also you can assign null. (Special Type)
Resource

//another side of variables type in php

data types in PHP

# 4 Scalar Types

  • bool
  • int
  • float
  • string

#4 Compound Types

  • array
  • object
  • callable
  • iterable

#2 Special Types

  • resource
  • null

those will be evaluated as false

// integers 0-0 = false

// floats 0.0 -0.0 = false

// = false =

//’0′ false

// [ ] = false

// null = false

PHP String Function and Work

FunctionWork
strlen()Count character
str_word_count()Count the word
strrev()Reverse the string
strrpos()Search the position of character/word (last)
stripos()Search the position of character/word (first)
strripos()Search the position of character/word (last) – Case Insensetive
str_repeat()Repeat the string

PHP and JavaScript, jQuery

Datatables Use This

SweetAlert Use This

PHP form with all type input


for radio type
==============
<input type="radio" value="male" name="gender" class="form-check-input">
<input type="radio" value="female" name="gender" class="form-check-input">

$gender = $_POST['gender'];



for checkbox
=========

  Cricket<input type="checkbox" value="cricket" name="sports[]" class="form-check-input">
  Football<input type="checkbox" value="football" name="sports[]" class="form-check-input">
  Badminton<input type="checkbox" value="badminton" name="sports[]" class="form-check-input">

$sports = $_POST['sports'];
$sports = implode(',', $sports) ;

It's will store the all checked data as separated by comma(,)

for date
=====
<input type="date" name="date" class="form-control my-3">

$date = $_POST['date'];

It's will store the date as year-month-date format (2020-02-20)



<input type="datetime-local" min="2023-07-22T12:00"  max="2017-06-30T16:30" name="date_time_local">

for color
======
<input type="color" name="color">
$color = $_POST['color'];


It's will store the color code as hexa format


If we print a file array

 $file = $_FILES['file'];

print_r($file)
https://prnt.sc/iR-wKQp-B1dh


pathinfo function

print_r(pathinfo('sujon.txt'))
https://prnt.sc/0eyvNJTGJc0x


Print the extension
$sujon =pathinfo('sujon.txt');
echo $sujon['extension'];


Easy method to get extension
echo pathinfo('sujon.txt',PATHINFO_EXTENSION);


File checking in server side
if($myfile['type'] != 'image/png'){
    echo "you need to upload png";
}

OOP with PHP

OOP is short form of Object Oriented Programming. Object oriented programming have four principle.

Four Principle of OOP

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Why we need OOP

Modularity (Encapsulation, Reusability)
Maintainability (Code Organization, Modifications)
Scalability (Extensibility, Flexible Architecture)
Abstraction
Reusability (Inheritance, Libraries and Frameworks)
Security (Controlled Access)
Polymorphism (Method Overriding)
Improved Collaboration
Real-World Modeling
Debugging and Testing

Four principle you can think like that:

  1. Encapsulation – making a capsule with hold manything
  2. Inheritance – extending functionality by copy other class functions
  3. Polymorphism – one function, one class can do multi works
  4. Abstraction – less specific

PHP Trait

<?php 




class BankAccount{
    public $accountNumber;
    public $accountBalance;

    public function setAccountNumber($account_no){
        $this->accountNumber = $account_no;
    }

    public function getAccountNumber(){
        echo "Account No: $this->accountNumber  <br>";
    }

    public function initiateAccountBalance($account_balance){
        $this->accountBalance = $account_balance;
    }

    public function getAccountBalance(){
        echo "Account Balance: $this->accountBalance  <br>";
    }
}


trait Bkash{
    public $bkashAccountNo;
    public $bkashBalance;

    public function setBkashAccountNo($bkash_no){
        $this->bkashAccountNo = $bkash_no;
    }

    public function getBkashAccountNo(){
        echo "Bkash Account No: $this->bkashAccountNo  <br>";
    }

    public function setbkashBalance($bkash_bal){
        $this->bkashBalance = $bkash_bal;
    }

    public function getbkashBalance(){
        echo "Bkash Account Balance: $this->bkashBalance  <br>";
    }
}

class Customer extends BankAccount{
    use Bkash;
    public $name;
    public function setName($name){
        $this->name = $name;
    }
    public function getName(){
        echo "Account Name: $this->name  <br>";
    }
}


$sujon = new Customer();

$sujon->setAccountNumber('1');
$sujon->initiateAccountBalance('100');
$sujon->setName('Sujon Kumar Dey');
$sujon->getName();
$sujon->getAccountNumber();
$sujon->getAccountBalance();
$sujon->setBkashAccountNo('01768203686');
$sujon->getBkashAccountNo('01768203686');
$sujon->setbkashBalance('500');
$sujon->getbkashBalance('500');




?>

Exclusive

header( "refresh:2;url=index.php" );

Spaceship operator <=>

isset() alternative - Null coalescing operator ??

Leave a Comment

Your email address will not be published. Required fields are marked *