Your SlideShare is downloading. ×
Introduction to php   basics
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Introduction to php basics

623
views

Published on

Published in: Technology

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
623
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
127
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Introduction to PHP
  • 2. PHP PHP Hypertext Pre-processor ‚PHP is a server side scripting language‛ “Personal home page”
  • 3. Generate HTML Get index.php 1 3 4 pass index.php to PHP interpretor 5 WebServer Index.php in interpreted HTMl form Browser 2 Get index.php from hard disk What you mean by hypertext Pre-processor?
  • 4. Scripting Languages • A ‚script‛ is a collection of program or sequence of instructions that is interpreted or carried out by another program rather than by the computer processor. Two types of scripting languages are – Client-side Scripting Language – Server-side Scripting Language • In server-side scripting, (such as PHP, ASP) the script is processed by the server Like: Apache, ColdFusion, ISAPI and Microsoft's IIS on Windows. • Client-side scripting such as JavaScript runs on the web browser.
  • 5. What you mean by server side scripting?? • Generate HTML Get index.php 1 3 4 pass index.php to PHP interpretor 5 WebServer Index.php in interpreted HTMl form Browser 2 Get index.php from hard disk PHP is server side scripting language because the php code runs on the server and returns the result in html form to the client browser
  • 6. What you mean by client side scripting?? • Generate HTML Get index.php 1 3 4 pass index.php to PHP interpretor 5 WebServer Index.php in interpreted HTMl form Browser 2 Get index.php from hard disk Client side scripting is something where code runs on client side(browser). For eg checking whether a HTML text field contains data or not can be done by running a script from browser itself
  • 7. Creating a PHP page
  • 8. How to create a PHP page 1. Install Wamp (Windows-Apache-Mysql-PHP) 2. Create a new folder within WampWWW folder with the name you want to give your project • Eg: C:WampWWWmyProject 3. Right click and Create a new text file with extention ‚.php‛ • Eg: index.php
  • 9. How to run PHP program 1. Start Wamp Server 2. Open your browser and type localhost/myProject/ 3. Most of the browsers load the file with name ‚index‛ automatically. So it is recommended to have the name ‚index‛ for your home page
  • 10. Getting started with PHP programming
  • 11. PHP Syntax  Structurally similar to C/C++  Supports procedural and object-oriented paradigm (to some degree)  All PHP statements end with a semi-colon  Each PHP script must be enclosed in the reserved PHP tag <?php //PHP code goes here.. ?>
  • 12. Other PHP tags <?php ... Code ?> Standard Tags <? ... Code ?> <?= $variable ?> <script language=“php”> ... Code </script> <% ... Code %> Short Tags Script TagsASP Tags
  • 13. Commenting // comment single line /* comment multiple lines */ //comment single line /* Comment multiple Lines*/ # comment single line-shell style C PHP
  • 14. Data Types
  • 15. Data types • PHP supports many different data types, but they are generally divided in two categories: • Scalar » boolean :A value that can only either be true or false » Int :A signed numeric integer value » float : A signed floating-point value » string : A collection of binary data • Composite. » Arrays » Objects
  • 16. Type Casting  echo ((0.1 + 0.7) * 10); //Outputs 8  echo (int) ((0.1 + 0.7) * 10); //Outputs 7 • This happens because the result of this simple arithmetic expression is stored internally as 7.999999 instead of 8; when the value is converted to int, PHP simply truncates away the fractional part, resulting in a rather significant error (12.5%, to be exact).
  • 17. Variables
  • 18. Declaring a variable //Declaring a variable Int a=10; Char c=‘a’; Float f=1.12; //Every variable starts with ‚$‚sign //No need of prior Declarations $a=10; $c=‘a’; $f=1.12; C PHP
  • 19. Variables • Variables are temporary storage containers. • In PHP, a variable can contain any type of data, such as, for example, strings, integers, floating numbers, objects and arrays. • PHP is loosely typed, meaning that it will implicitly change the type of a variable as needed, depending on the operation being performed on its value. • This contrasts with strongly typed languages, like C and Java, where variables can only contain one type of data throughout their existence.
  • 20. Variable Variables • In PHP, it is also possible to create so-called variable variables. That is a variable whose name is contained in another variable. For eg: $name = ’foo’; $$name = ’bar’; echo $foo; // Displays ’bar’ function myFunc() { echo ’myFunc!’; } $f = ’myFunc’; $f(); // will call myFunc();
  • 21. Determining If a Variable Exists • There is an inbuilt method called isset() which will return true if a variable exists and has a value other than null $x=12; echo isset ($x); // returns true echo isset($y); // returns false
  • 22. Constants
  • 23. Declaring a constant //Declaring a constant using ‘const’ const int a=10; int const a=10; //Declaring a constant using ‘#define’ #define TRUE 1 #define FALSE 0 #define NAME_SIZE 20 define(’name’, ’baabtra’); define(’EMAIL’, ’davey@php.net’); echo EMAIL; // Displays ’davey@php.net’ echo name.PHP_EOL.email; // Displays ’baabtra; ‘davey@php.net’ C PHP
  • 24. Constants • Conversely to variables, constants are meant for defining immutable values. • Constants can be accessed for any scope within a script; however, they can only contain scalar values. • The built-in PHP_EOL constant represents the ‚end of line‛ marker.
  • 25. OutPut
  • 26. output Int a=10,b=25; Printf(‚%d %d‛,a,b); $a=10; $b=25 Printf(‚%d %d‛,$a,$b); print $a; print $b; //Cannot take multiple arguments echo $a,$b; //Fast and commonly used die(‚the end‛); exit(‚the end‛); // both allows to terminate the script’s output by outputting a string C PHP
  • 27. echo Vs print • echo is a language construct ( Constructs are elements that are built-into the language and, therefore, follow special rules) • echo doesnt have a return value • print() is not a language construct. It is an inbuilt function only • Always return 1 once it print someting
  • 28. Control structures
  • 29. Control Structures Conditional Control Structures • If • If else • Switch Loops For While Do while Other • Break • Continue ‚Exactly the same as on left side‛ C PHP
  • 30. functions
  • 31. Functions Int findSum(int a,int b) { Int c; c=a+b; Return c } findSum(10,15); function findSum($a,$b) { $c=$a+$b; Return $c; } findSum(10,15); C PHP
  • 32. Function arguments • You can define any number of arguments and, in fact, you can pass an arbitrary number of arguments to a function, regardless of how many you specified in its declaration.PHP will not complain unless you provide fewer arguments than you declared Eg: function greeting($arg1) { echo ‚hello $ arg1‛; } greeting(‚baabtra‛); greeting(‚baabtra‛, ‛baabte‛); greeting(); // ERROR
  • 33. Function arguments • You can make arguments optional by giving them a default value. function hello($who = "World") { echo "Hello $who"; } hello(‘baabtra’); // hello baabtra hello(); // hello world
  • 34. Function arguments PHP provides three built-in functions to handle variable-length argument lists: – func_num_args() : return the total number of arguments – func_get_arg() : return the argument at any index position – func_get_args() : returns an array of all argument
  • 35. Function arguments function hello() { if (func_num_args() > 0) { $arg = func_get_arg(0); // The first argument is at position 0 echo "Hello $arg"; } else { echo "Hello World"; } } hello("Reader"); // Displays "Hello Reader" hello(); // Displays "Hello World“
  • 36. Function return type • In PHP functions even if you don’t return a value, PHP will still cause your function to return NULL. So there is no concept of ‘void’
  • 37. Strings Char a*+=‚Baabtra‛; Printf(‚Hello %s‛,a); $a=‚Baabtra‛; echo ‚Hello $a‛; //Output: Hello baabtra; echo ‘Hello $a’; //Outputs : Hello $a ‚Strings will be continued in coming chapters‛ C PHP
  • 38. Arrays Indexed Array int a[]={1,2,3,4,5,6,7,8,9,10}; for(i=0;i<10;i++) Printf(‚%d‛,a[i]); Indexed Array / Enumerated array $a=array(1,2,3,4,5,6,7,8,9,10) for($i=0;$i<10;$i++) echo $a[$i] ; Associative Array $a*‘name’+=‚John‛; $a*‘age’+=24; $a*‘mark’+=35.65; ‚Array will be continued in coming chapters‛ C PHP
  • 39. Operators
  • 40. Operators C Arithmetic Operators +, ++, -, --, *, /, % Comparison Operators ==, !=, ===, !==, >, >=, < , <= Logical Operators &&, ||, ! Assignment Operators =, +=, -=, *=, /=, %= PHP ‚Supports all on left . In addition PHP supports below operators as well‛ • Execution Operators • String operators • Type Operators • Error Control Operators • Bitwise Operators
  • 41. • The backtick operator makes it possible to execute a shell command and retrieve its output. For example, the following will cause the output of the UNIX ls commandto be stored inside $a: Eg : $a = ‘ls -l‘; Execution operator (‘) Don’t confuse the backtick operator with regular quotes (and, conversely, don’t confuse the latter with the former!)
  • 42. – The dot (.) operator is used to concatenate two strings Eg1: $string=‚baabtra‛. ‚mentoring parner‛ Eg2: $string1=‚baabtra‛; $string2=‚mentoring parner‛; $string3=$string1.$string2; String operators (.)
  • 43. – Used to determine whether a variable is an instantiated object of a certain class or an object of a class that inherits from a parent class Eg: class ParentClass { } class MyClass extends ParentClass { } $a = new MyClass; var_dump($a instanceof MyClass); // returns true as $a is an object of MyClass var_dump($a instanceof ParentClass); // returns true as $a is an object of MyClass which inherits parentClass Type operators (instanceof )
  • 44. Error suppression operator (@) • When prepended to an expression, this operator causes PHP to ignore almost all error messages that occur while that expression is being evaluated Eg: $x = @mysql_connect(‘localhost’,’root’,’1234’); • The code above will prevent the call to mysql_connect() from outputting an error—provided that the function uses PHP’s own functionality for reporting errors. • Sadly, some libraries output their errors directly, bypassing PHP and, therefore, make it much harder to manage with the error-control operator.
  • 45. • Bitwise operators allow you to manipulate bits of data. All these operators are designed to work only on integer numbers—therefore, the interpreter will attempt to convert their operands to integers before executing them. Bitwise Operators & Bitwise AND. The result of the operation will be a value whose bits are set if they are set in both operands, and unset otherwise. | Bitwise OR. The result of the operation will be a value whose bits are set if they are set in either operand (or both), and unset otherwise. ˆ Bitwise XOR (exclusive OR). The result of the operation will be a value whose bits are set if they are set in either operand, and unset otherwise. << Bitwise left shift. This operation shifts the left-hand operand’s bits to the left by a number of positions equal to the right operand, inserting unset bits in the shifted positions >> Bitwise right shift. This operation shifts the left-hand operand’s bits to the right by a number of positions equal to the right operand, inserting unset bits in the shifted positions.
  • 46. Questions? ‚A good question deserve a good grade…‛
  • 47. End of day
  • 48. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com