Introduction To PHP: Iii I.T
Introduction To PHP: Iii I.T
Introduction To PHP: Iii I.T
T Introduction to PHP
PHP, or PHP: Hypertext Preprocessor, is a cross-platform server side scripting language that is primarily used to create dynamic and interactive web pages, which means that it can be used on almost every major operating system. It can be embedded into HTML and is generally run on web servers that are running the Apache HTTP server software. It is free software and is free to download and use. PHP is a recursive acronym for "PHP: Hypertext Preprocessor". PHP is an open source, server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire ecommerce sites. PHP is compatible with all the major web servers like Apache, Microsoft IIS. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support for Java and distributed object architectures (COM and CORBA), making n-tier development a possibility for the first time. PHP Syntax is very similar to C and Perl languages.
Characteristics of PHP
Five important characteristics make PHP's practical nature possible: Simplicity Efficiency Security Flexibility Familiarity Familiarity Programmers from many backgrounds will already be familiarized with the PHP language. Many of the languages constructs are borrowed from C and Perl, and in many cases PHP code is almost indistinguishable from the typical C program. Simplicity There is no need to include libraries, special compilation directives, or anything. The PHP engine simply begins executing the code after the first escape sequence (<?php). If the code is syntactically correct, it will be executed exactly as it is displayed.
1. The user enters a web page address in the browsers location bar. 2. The browser breaks apart that address and sends the name of the page to the host. For example, http://www.phone.com/login.php requests the page login.php from www.phone.com. 3. The web server process on the host receives the request for login.php. 4. The web server reads the login.php file from the hosts hard drive. 5. The web server detects that the PHP file isnt just a plain HTML file, so it asks another processthe PHP interpreterto process the file.
PHP Environment
In order to develop and run PHP Web pages, three vital components need to be installed on our computer system. Web Server - PHP will work with virtually all Web Server software, including Microsoft's Internet Information Server (IIS) but then most often used is freely available Apache Server. Database - PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database. PHP Parser - In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser.
XAMPP Installation
XAMPP is an easy-to-use multi-platform package that installs Apache, MySQL, PHP, phpMyAdmin, and other software useful for dynamic web development on the computer. Installation is painless, and configuration minimal. There are some steps to be followed for installing XAMPP in Windows 1. Download the Basic Package XAMPP MSI installer found http://www.apachefriends.org/en/xampp-windows.html. 2. Double-click the MSI installer file on the desktop, and well see the installer.
at
3. Select English and click the OK button. 4. The Setup Wizard appears as below figure. Click Next.
5. The below dialog box is displayed. Click Next to accept the default installation directory.
6. The XAMPP Options dialog displays. Leave the Service section checkboxes unchecked so we dont install the components as services; instead, well start them from the Control Panel. Click Install.
7. The Completing the XAMPP Setup Wizard displays. Click Finish. 8. The option to start the Control Panel displays as the below figure. Click Yes.
9. The Control Panel launches, as shown below. The Control Panel can be used to start and stop the services, as well as aid in their configuration
PHP Syntax
The syntax for PHP scripting blocks start with a <?php delimiter and end with a ?> delimiter. A PHP scripting block can be placed anywhere within an HTML document. If PHP is included within an HTML document, the document must have a .php extension. <?php // PHP scripting block ?> There are other PHP delimiters that can be used, however, it is recommended that you use the standard form <?php and ?> delimiters for maximum compatibility. Other PHP delimiters that we can use are Shorthand delimiters (<? ?>). ASP style delimiters (<% %>) Placing our code inside <script> tags with the language attribute set to php. The examples below show us how each style of PHP delimiter looks. On servers that have shorthand support enabled, we can use the scripting block delimiters <? and ?>. Syntax: <? // PHP code ?> We can use the ASP style delimiters <% and %> by turning on the asp_tags directive. Syntax: <% // PHP code %> We can also place PHP code within the <script> and </script> tags with the language attribute set to php Syntax <script language="php"> // PHP code </script> Each PHP statement must end with a semi-colon (;). This tells the PHP interpreter that that PHP statement has ended. If a semi-colon is not included at the end of the line of code, the interpreter will assume that the PHP statement continues onto the next line. The PHP interpreter condenses all sequential whitespace in PHP scripts to a single whitespace. This allows programmers to structure their code into a more readable format.
Step 3: Open any web browser(Chrome, IE, Firefox, Safari, Opera etc.) . Simply type http://localhost/myFirstPHPScript.php in the address bar. Then we get the page like
PHP Variables
Variables are used for storing values, such as numbers, strings or function results, so that they can be used many times in a script. Once a variable is declared, it can be used over and over again
throughout your script. Declaration of Variables in PHP To declare a variable in PHP, first type the dollar sign symbol ($), followed by the name of the variable. The name of the variable is followed by the assignment operator (=), which is followed by the value that you want to assign to the variable, followed by the semicolon (;). Variable Naming Conventional and Rules A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, _) A variable name should not contain spaces. If a variable name should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString) The variable names are case-sensitive, so $myvar is different from $myVar.
The syntax of PHP variables is <?php $variable_name = value; ?> Variable Types PHP has a total of eight data types which we use to construct our variables. Out of eight data types, four are scalar types, two are compound types and other two are special types. Scalar Data types: Integers: are whole numbers, without a decimal point, like 4195. Doubles: are floating-point numbers, like 3.14159 or 49.1. Booleans: have only two possible values either true or false. Strings: are sequences of characters, like 'PHP supports string operations.
PHP Constants
A PHP constant is a variable that cannot be changed after the initial value is assigned during the execution of the script. We can create a constant variable using the define () function. The function accepts 2 arguments. Syntax: define (<constant name>, <value>); The first must be a string and represents the name which will be used to access the constant. A constant name is case-sensitive by default and by convention, is always uppercase. A valid constant name starts with a letter or underscore, followed by any combination of letters, numbers or underscores. Constant names must be in quotes when they are defined. The second argument is the value of the constant and can be a string or numbers and can be set explicitly. The scope of a constant is global, which means that we can access constants anywhere in our script without regard to scope. <?php define ("WEBSITE", "WebDevelopmentTutorials.com"); define(PI,3.17); echo WEBSITE; EchoPI value is:.PI;?>
Operator =
Description Simple assignment operator, Assigns values from right side operands to left side operand Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandAND assignment operator, It Divide divides left operand with the right operand and assign the result to left operand Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
+=
-=
C -= A is equivalent to C = C - A
*=
C *= A is equivalent to C = C * A
/=
C /= A is equivalent to C = C / A
%=
C %= A is equivalent to C = C % A
PHP Arithmetic Operators PHP arithmetic operators are used to perform mathematical operations. +(addition) operator The +(addition) operator calculates the sum of 2 values. <?php echo 1 + 1; ?>
PHP String Concatenation Operator The PHP concatenation operator (.) is used to combine 2 sting values to create one string. The concatenation operator us useful when WE are combining strings with values from constants and arrays. <?php $variable1="Hello"; $variable2="World"; echo $variable1 . " " . $variable2; ?> The code above outputs: Hello World
PHP Increment and Decrement Operators PHP increment and decrement operators are used to increment and decrement numeric values. ++$value(pre-increment) operator The PHP ++$value(pre-increment) operator adds 1 to a value before the value is used in the expression in which it is contained. <?php $value = 1; echo ++$value; ?> The code above outputs: 2
Operator and
Description Called Logical AND operator. If both the operands are true then then condition becomes true. Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. Called Logical AND operator. If both the operands are non zero then then condition becomes true. Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
or
(A or B) is true.
&&
(A && B) is true.
||
(A || B) is true.
B)
is
!=
>
<
(A < B) is true.
>=
<=