Friday, July 7, 2017

PHP Basics

PHP-MySQL

PHP Basics

Content

* Introduction to PHP 
* Installation
* PHP Syntax
* Saving and Executing PHP page
* Comments
* Variables
* Operators

Introduction to PHP

* PHP is server side scripting language
* Earlier it was Personal Home Page.
* Now a days it is PHP: Hypertext Preprocessor
* PHP is a powerful tool for making dynamic and interactive web pages.
* It was created in 1994 by Rasmus Lerdorf 
* PHP is a language for creating interactive web sites and is used on over 3.3 million web sites around the world. 
* PHP files can contain text, HTML tags and scripts 
* PHP files are returned to the browser as plain HTML. 
* PHP files have a file extension of ".php" 
* PHP can run on different platforms (windows, Linux, Unix, etc)
* PHP is compatible with almost all servers used today (Apache, IIS, etc.)
 
Installation

* You have to install three things
 - PHP
 - MySQL 
 - Apache Server
* Another Option is to install WAMP( W: Windows A:  Apache M: MySQL P: PHP )
* Download WAMP from www.wampserver.com
* WAMP is Windows Apache MySQL PHP

PHP Syntax

* A PHP scripting block always starts with 
* A PHP scripting block can be placed anywhere in the document. 
* A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
* The PHP script is executed on the server, and the plain HTML result is sent back to the browser.
* Each code line in PHP must end with a semicolon.
* There are two basic statements to output text with PHP: echo and print.
* The file must have a .php extension. 
* If the file has a .html extension, the PHP code will not be executed. 
* Whenever we create a new PHP page, we need to save it in our web servers root directory.
* For WAMP it is a folder called  www folder and is usually at this location on our hard drive:
c:\wamp\www

Saving and Executing PHP page

<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Welcome To PHP";
?>
</body>
</html>

1.Save the previous page by the name "demo.php" in the www folder of wamp.
2.Open the browser.
3.Type the url : " http://localhost/demo.php"
Comments
* Comments:  Comments are place in the same fashion as we do in C or C++. You can use single line comment by using //. And multi-line comment using /*...*/
Variables
* Variables are used to store constants 
* These constants could be strings, numbers or arrays. 
* When a variable is declared, it can be used over and over again in your script. 
* All variables in PHP start with a $ sign symbol


* In PHP, the variable is declared automatically when you use it.
* A variable name must start with an alphabet or an underscore "_"
* A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
* A variable name should not contain spaces. 
* In PHP, a variable does not need to be declared before adding a value to it. 
* PHP automatically converts the variable to the correct data type, depending on its value

Operators

Concatenation Operator
* There is only one string operator in PHP, it is the concatenation operator (.)  is used to put two string values together.


Arithmetic Operators

Operator  ==> Name  ==> Example  ==> Result
* +  ==> Addition  ==> $x + $y  ==> Sum of $x and $y
* -  ==> Subtraction  ==> $x - $y  ==> Difference of $x and $y
* *  ==> Multiplication  ==> $x * $y  ==> Product of $x and $y
* /  ==> Division  ==> $x / $y  ==> Quotient of $x and $y
* %  ==> Modulus  ==> $x % $y  ==> Remainder of $x divided by $y
* Operator  ==> Name  ==> Description
* ++$x  ==> Pre-increment  ==> Increments $x by one, then returns $x
* $x++  ==> Post-increment  ==> Returns $x, then increments $x by one
* --$x  ==> Pre-decrement  ==> Decrements $x by one, then returns $x
* $x--  ==> Post-decrement  ==> Returns $x, then decrements $x by one


Assignment Operators

Assignment  ==> Same as...  ==> Description
* x = y  ==> x = y  ==> The left operand gets set to the value of the expression on the right
* x += y  ==> x = x + y  ==> Addition
* x -= y  ==> x = x – y  ==> Subtraction
* x *= y  ==> x = x * y  ==> Multiplication
* x /= y  ==> x = x / y  ==> Division
* x %= y  ==> x = x % y  ==> Modulus

Relational Operators

Operator  ==> Name  ==> Example  ==> Result
* ==  ==> Equal  ==> $x == $y  ==> True if $x is equal to $y
* ===  ==> Identical  ==> $x === $y  ==> True if $x is equal to $y, and they are of the same type
* !=  ==> Not equal  ==> $x != $y  ==> True if $x is not equal to $y
* <>  ==> Not equal  ==> $x <> $y  ==> True if $x is not equal to $y
* !==  ==> Not identical  ==> $x !== $y  ==> True if $x is not equal to $y, or they are not of the same type
* >  ==> Greater than  ==> $x > $y  ==> True if $x is greater than $y
* <  ==> Less than  ==> $x < $y  ==> True if $x is less than $y
* >=  ==> Greater than or equal to  ==> $x >= $y  ==> True if $x is greater than or equal to $y
* <=  ==> Less than or equal to  ==> $x <= $y  ==> True if $x is less than or equal to $y

Logical Operators

Operator  ==> Name  ==> Example  ==> Result
* and  ==> And  ==> $x and $y  ==> True if both $x and $y are true
* or  ==> Or  ==> $x or $y  ==> True if either $x or $y is true
* xor  ==> Xor  ==> $x xor $y  ==> True if either $x or $y is true, but not both
* &&  ==> And  ==> $x && $y  ==> True if both $x and $y are true
* ||  ==> Or  ==> $x || $y  ==> True if either $x or $y is true
* !  ==> Not  ==> !$x  ==> True if $x is not true


<<Prev     Home     Next>>

No comments:

Post a Comment