Docsity

All about Php and SQL, Lecture notes of Information Technology

Toggle sidebar
bg1
PHP
•PHP (recursive acronym for "PHP: Hypertext
Preprocessor") is a widely-used Open Source
general-purpose scripting language that is
especially suited for Web development and
can be embedded into HTML.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download All about Php and SQL and more Lecture notes Information Technology in PDF only on Docsity!

PHP

  • (^) PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

How PHP came into being

  • (^) PHP started as a quick Perl hack written by Rasmus Lerdorf in late 1994. Over the next two to three years, it evolved into what we today know as PHP/FI 2.0. PHP/FI started to get a lot of users, but things didn't start flying until Zeev Suraski and Andi Gutmans suddenly came along with a new parser in the summer of 1997, leading to PHP 3.0. PHP 3.0 defined the syntax and semantics used in both versions 3 and 4.

PHP Code

  • (^) To give you an idea of what embedding PHP would entail, consider the following three "hello world" examples, all of which will give the exact same output:
  • (^) Ex. 1: (HTML alone) Hello, World!
  • (^) Ex. 2: (PHP code alone )
  • (^) Ex. 3: (PHP embedded within HTML) World!

Dynamic vs. Static Web pages

  • (^) The "Hello, World" example we chose would certainly not require you to use PHP. That's because it is static, meaning its display will always remain the same. But what if you wanted to greet the world in any number of ways? Say, for example, "Bonjour, World!", or "Yo, World!" and so on.
  • (^) Since HTML tags are purely descriptive they cannot function as a variable. Nor can they convey even the simplest of uncertainty such as a "Bonjour" or a "Yo". You need a command language to handle variability in a Web page. Based on either a conditional statement or direct user input, a command language can generate the "static" HTML necessary to correctly display a Web page's content.

Variables In PHP

  • (^) a variable does not require formal declaration. It will automatically be declared when a value is assigned to it. Variables are prefixed by a dollar sign : ($VariableName). Variables do not have declared types. A variable's type does not have to be fixed, meaning it can be changed over the variable's lifetime. The table below list's PHP's variable types:

Variable in PHP

Type Description

  • (^) String - constants may be enclosed between double- quotes ( " ) or single-quotes ( ' )
  • (^) Integer -integer number
  • (^) Double -floating point number
  • (^) bool -Boolean (true or false), available from PHP
  • (^) Array -hybrid of ordered array and associative array
  • (^) object -an object with properties and methods (not discussed in this article

String

  • (^) The string constants may be enclosed between double-quotes ( " ) or single-quotes ( ' ). In a double-quote enclosed string, the variable conversion is done automatically. $city = "Ankara"; print "I live in $city."; $height = 182; print "I am $height cm tall."; String operators : Concatenation:. Concatenation to the end: .= $str1 = $lastname. ", ". $firstname; $str2 .= $str3;

Array

  • (^) PHP supports two type of arrays:
  • Numerically indexed arrays
  • Associative arrays.

Associative Arrays

  • (^) Associative arrays has two part: keys and values. The keys are indices of the array. Both the keys and values can be of any data type in the same array.
  • (^) => operator is used to combine the key and the value of an element of an associative array. Creation of associative arrays: $as1 = array(24 => "Ali", "Ankara" => 844, "Pi" => 3.14159); $as2 = array(20458444 => "Ali Ak", 20487542 => "Ayse Bal"); Accessing array contents: print $as1["Ankara"]; // outputs: 844 $as1[24] = "Ayse"; // value of element with key=24 is changed. $as2[20608888] = "Fatma Girik"; // a new element is added to array. foreach ($as2 as $id => $name) { print "$id: $name
    "; } // all keys and values are used foreach ($as4 as $k => $v) { $as4[$k] = $v + 7; } // all values of array $as4 are increased by 7

Conditionals and Looping Constructs

  • (^) PHP includes if and elseif conditionals, as well as while and for loops, all with syntax similar to C. The example below introduces these four constructs:

Conditionals and Looping Constructs

  • (^) FOR Loop Statement for ($j = 0; $j < 10; $j++) { print "j = $j
    \n"; }

Conditionals and Looping Constructs

  • (^) WHILE Loop Statement while ($d) { print "ok
    \n"; $d = test_something(); }

Operators

  • (^) Arithmetic Operators Example Name Result $a + $b Addition Sum of $a and $b. $a - $b Subtraction Difference of $a and $b. $a * $b Multiplication Product of $a and $b. $a / $b Division Quotient of $a and $b. $a % $b Modulus Remainder of $a divided by $b.

Operators

  • (^) Incrementing/Decrementing Operators Example Name Effect ++$a Pre-increment Increments $a by one, then returns $a. $a++ Post-increment Returns $a, then increments $a by one. --$a Pre-decrement Decrements $a by one, then returns $a. $a-- Post-decrement Returns $a, then decrements $a by one.