1.4k likes | 1.41k Views
Learn about PHP, a server-side scripting language used to develop dynamic web applications.We will cover installation, configuration, and basic PHP syntax.
E N D
Introduction to PHP • PHP Hypertext Preprocessor • Personal Home Page • Server-side scripting language • Used to develop dynamic web application • Free and open source • Can embed PHP scripts inside HTML • PHP statements enclosed in <?php and ?> tags • If short tags are enabled on the server, you can use <? and ?>
Introduction to PHP • Browser will only get the php output code, if there is any • Syntax is similar to C • PHP doesn’t need the indenting, but it helps humans read the code.
Server-side Scripting • Runs the scripts on the web server • enables the ability to highly customize the response based on the user's requirements, access rights, or queries into data stores.
Server-side scripting technologies • ASP • JSP • PHP • Ruby on Rails
Processing PHP Files • PHP pages cannot be directly viewed using a browser. • Instead, you must deploy the php page in a web server and make request to the php page which resides in the web server using the browser. • If the requested web page only contains pure HTML, web server directly sends the HTML page to the web browser • If the requested web page contains a PHP code (<?php), web server sends the php code to the PHP processor (PHP Engine) and sends the processed HTML output to the browser
Displaying content on the web • Can use echo or print statements • E.g. • echo outputitem,outputitem,outputitem,...; • outputitemcan be a number, a string, or a variable • echo “Hello”; • echo 123; • echo ‘Hello’,’World!’; • echo Hello World!; // Not valid
Configuring the environment - Installing XAMPP • Download from www.apachefriends.org/en/xampp-windows.html • Contains, • MySQL • PHP • Apache • phpMyAdmin • It’s best to accept the default location - c:\xampp (on Windows Vista, 7, or 8, you cannot install in the Program Files folder because of a protection problem)
Configuring the environment - Installing XAMPP • Under Service Section, select the Install Apache as Service and the Install MySQL as Service check boxes. (causes them to start automatically when the computer starts) • Then click install • Enter localhost / 127.0.0.1 in a browser • For phpMyAdmin , Click the phpMyAdmin link in • the Tools section toward the bottom of the left panel
XAMPP Control Panel • Start -> All Programs -> Apache Friends -> XAMPP -> XAMPP Control Panel • need to restart Apache whenever you make changes to your PHP configuration
Testing PHP • Web space – a place where Apache looks for your scripts when you type localhost (c:\xampp\htdocs) • can change the location of web space in the Apache configuration file. • Open a notepadand type below code <html> <head><title>PHP test</title></head> <body> <?php phpinfo(); ?> </body> </html>
Testing PHP • Save the file with the name test.php in your web space and localhost/test.php on the browser
Testing PHP • Save the file with the name test.php in your web space and localhost/test.php on the browser
Configurations • Configuring PHP • PHP looks for php.ini when it begins and uses the settings that it finds. • If PHP can’t find the file, it uses a set of default settings • c:\xampp\apache\bin\php.ini • Need to restart after making changes
Configurations • Configuring apache • configuration settings are stored in a file named httpd.conf. • c:\xampp\apache\conf\httpd.conf • can change some of Apache’s behavior with directives in the httpd.conf file (E.g. where Apache looks for web page files, what port number Apache listens on, etc) • Configuring MySQL • MySQL configuration file is c:\xampp\mysql\bin\my.cnf
Configurations • 1. Stop both Apache and MySQL in the XAMPP Control Panel • 2. Start the uninstall by choosing Start -> All Programs -> Apache Friends -> XAMPP -> Uninstall. • 3. Move through the screens and answer the questions. • can save any databases or web pages you have created by selecting the appropriate options • 4. Start the installation procedure again from the beginning
variables • containers used to hold information • A variable has a name, and information is stored in the variable • One of the most common uses for variables is to hold the information that a user types into a form
Naming a variable • Identifier: All variable names have a dollar sign ($) in front of them. This tells PHP that it is a variable name. • Beginning of name: Variable names must begin with a letter or an underscore. They cannot begin with a number. • Acceptable length: Variable names can be any length. • Acceptable characters: Variable names can include letters, numbers, and underscores only. • Case sensitivity: Uppercase and lowercase letters are not the same.($firstname and $Firstname are not the same variable; i.e. case sensitive) • Use meaningful names for variables
assigning values to variables • Must use = sign • $age = 12; • $price = 2.55; • $number = -2; • $name = “Jhone”; • $name = “Bob”; // does not create a new variable. Replaces the value Jhone with Bob • $name = “”; //The variable $age exists but doesn’t contain a value • unset($age); // variable $age no longer exists.
Using variable variables • dynamic variable names . • Mainly used with arrays and loops • can name a variable with the value stored in another variable (one variable contains the name of another variable) • $name_of_the_variable = “city”; • $$name_of_the_variable = “Los Angeles”; • $city = “Los Angeles”; // Final result • $$ - means variable variable
Using variable variables $Reno = 360000; $La = 138000; $cityname = “Reno”; echo “The size of $cityname is ${$cityname}”; $cityname = “La”; echo “The size of $cityname is ${$cityname}”; • need to use curly braces around the variable name in the echo statement
Using variable variables Without the curly brackets $Reno = 360000; $cityname = “Reno”; echo “The size of $cityname is $$cityname”; The size of Reno is $Reno
Displaying variable values • Can use : echo, print, print_r, var_dump
Using print and echo statements $string1 = “Hello”; $string2 = “World!”;
Note • Single quotes (‘ ‘): When you use single quotes, variable names are echoed as is. • Double quotes (“ “): When you use double quotes, variable names are replaced by the variable values. • Sometimes you need to enclose variable names in curly braces ({ }) to define the variable name. $pet = “bird”; echo “The $petcage has arrived.”; • Above code will give you an error since it can’t find a variable called petcage
Note (Cont.) $pet = “bird”; echo “The {$pet}cage has arrived.”; • The above code will output “The birdcage has arrived.” • A variable keeps its information for the entire script (until the end of the page, if not defined inside a code block) – global scope
Using print_r • PHP built-in function. • E.g. $weekday = “Monday”; print_r($weekday);
Using var_dump • use to display a variable value and its data type • Mainly used in debugging (troubleshooting) • E.g. • $weekday = “Monday”; • var_dump($weekday); • o/p : string(6) “Monday”
PHP Constants • Same as variables • Value never changes • Constant names are not preceded by a dollar sign define(“constantname”,”constantvalue”); define(“COMPANY”, “Matrix”); echo COMPANY; // must not use quotes • can store either a string or a number define (“AGE”, 29);
Data types in PHP • PHP has 8 data types • Integer: A whole number • Floating-point number (float): A numeric value with decimal digits • String: A series of characters • Boolean: A value that can be either true or false • NULL: A value that represents no value • Array: A group of values in one variable • Object: A structure created with a class • Resource: A reference that identifies a connection
Notes – Data types • PHP determines the data type automatically (loosely typed) $var1 = 123; // an integer $var2 = “123”; // string • PHP converts data types automatically • Can use casting – changing the data type $var3 = “222”; $var4 = (int) $var3; • can query the data type var_dump($var4); // int(222)
integers and floating-point numbers • PHP enables you to do arithmetic operations on numbers. $n1 = 1.5; $n2 = 2; $sum = $n1 + $n2;
Arithmetic operators • + , - , *, /, % • Note: PHP does multiplication and division first, followed by addition and subtraction. If other considerations are equal, PHP goes from left to right. (can change the order parentheses) • $result = 1 + 2 * 4 + 1;
Formatting numbers • In PHP if the number is 10.00, it’s displayed as 10 • Can use sprintf(), number_format() methods to format numbers • E.g. $newvariablename = sprintf(“%01.2f”, $oldvariablename); // adds 2 decimal points, return type is String $f_price = number_format($price,2); //adds commas to separate thousands and two decimal places
Strings in PHP • a series of characters • $string = “Hello World!”; • $string = ‘Hello World!’; • Single-quoted strings are stored literally — with the exception of \’, which is stored as an apostrophe. • In double-quoted strings, variables and some special characters are evaluated before the string is stored.
Strings in PHP - Example • E.g. • $month = 12; • $result1 = “$month”; • $result2 = ‘$month’; • echo $result1; • echo “<br />”; • echo $result2; • $string1 = “String in \ndouble quotes”; • $string2 = ‘String in \nsingle quotes’; • \t – inserts a tab
Strings in PHP - Example • $number = 10; • $string1 = “There are ‘$number’ people in line.”; • $string2 = ‘There are “$number” people waiting.’; • echo $string1,”<br />\n”; • echo $string2; • OP: There are ‘10’ people in line. There are “$number” people waiting.
Joining Strings • Can concatenate strings using dot . $string1 = ‘Hello’; $string2 = ‘World!’; $string3 = $string1.$string2; $string4 = $string1.” “.$string2; echo $string3; echo $string4;
Joining Strings (Cont.) • can use .= to add characters to an existing string $stringall = “Hello”; $stringall .= “ World!”; echo $stringall;
Long strings in PHP - heredoc • A heredocenables you to tell PHP where to start and end reading a string $varname = <<<ENDSTRING text ENDSTRING; // ENDSTRING can have any name
Boolean datatype • $var1 = true; • following values are evaluated as false • The word false • The integer 0 • The floating-point number 0.0 • An empty string • A string with the value 0 • An empty array • An empty object • The value NULL • Note: If a variable contains a value , it is assigned the value true.
NULL datatype • A variable with a NULL value contains no value • $var1 = NULL;