110 likes | 281 Views
Introduction to PHP. A user navigates in her browser to a page that ends with a .php extension The request is sent to a web server, which directs the request to the PHP interpreter The PHP interpreter processes the page, communicating with file systems, databases, and email servers as necessary
E N D
Introduction to PHP • A user navigates in her browser to a page that ends with a .php extension • The request is sent to a web server, which directs the request to the PHP interpreter • The PHP interpreter processes the page, communicating with file systems, databases, and email servers as necessary • Finally delivers a web page to the web server, which is served to the browser
PHP code can be inserted into HTML files using a special set of tags Standard tags <?php . . . code ?> Short tags <? . . . code ?> <? =$variable ?> Script tags <script language=“php”> . . . code </script> ASP tags <% . . . code %> asp_tags in php.ini Short, script and ASP tags are considered deprecated and their usage is strongly discouraged Syntax
PHP comments Commenting is important in any programming language to document the behaviour of the code. Otherwise maintenance becomes difficult. Supports C, C++ and Unix shell style (Perl style) comments //one line C++ style comment /* this is a multi-line comment. Yet another line of comment */ # one line shell style comment d1comments.php source d1comments.php Comments
Various types of datatypes in PHP Scalar Contains only one value at a time PHP supports four scalar datatypes boolean int float string Compound array object Special types resource null Datatypes [1]
Scalar data types Numericvalues integer Used to represent signed integers Can use decimal, octal and hexadecimal notations Start with 0 for octal (base 8) and 0x for hexadecimal (base 16) notation float Are numbers that have a fractional component Two different notations to express them Decimal (e.g. 0.12, 1234.43) Exponential (e.g. 2E7, 1.2e2) String Series of characters within PHP, each character is represented by a single byte Upto PHP 5, no support for multi-byte character sets PHP does not impose any limit on length of strings 3 ways of string output Boolean They hold only one of two possible values true or false Any integer other than 0 is cast to TRUE TRUE & FALSE are case insensitive, though the all capitals representation is widely used Datatypes [2]
Compound Essentially container(s) for multiple data elements / data and code Array Containers of multiple ordered data elements Can be used to store and retrieve any other data type, including numbers, boolean values, strings, objects and even arrays Covered in more detail in a subsequent section Object Objects are containers of data and code They allow a cohesive combination of data and code They are the fundamental blocks of OOP (Object Oriented Programming) Datatypes [3]
Special datatypes Null A null variable is special, as it has no type and no value NULL is not the same as integer zero nor the zero length string (both have types) A variable is considered NULL if it is assigned the constant NULL, or it is not set to any value as yet or it has been unset (using unset()). Resource Resources are special variables. They hold references to external resources (some sort of operating system resource such as an open file or database connection) Resources are created by using special functions, depending on the type of the resource Functions is_resource() and get_resource_type() are commonly used with resources Datatypes [4]
Constants Are identifiers for immutable values (viz., the value cannot be changed in the course of execution of the script) Constants are created using the define() function and are by convention all in uppercase letters Constants can be accessed from anywhere on the page No need to use $ in front of the constant name, as in the case of a variable Variables Are temporary storage containers In PHP, a variable can contain any type of data. PHP is loosely typed, meaning it will change the type of a variable as needed Variables are identified by $ sign, followed by an identifier name isset()is used to determine whether a variable exists or not Other relevant functions are unset() and empty() d1datatype.php source d1datatype.php Constants & variables
Assignment For assigning data to variables (assignment with operation as well) Arithmetic For performing basic mathematic functions String For joining two or more strings Comparison For comparing two values (i.e. pieces of data). Various rules govern the comparison process Logical For performing logical operations on boolean values Bit-wise For manipulating bits using boolean algebra Error control For suppressing errors Execution For executing system commands Increment / Decrement For incrementing / decrementing numeric values Type For identifying operators d1operator.php source d1operator.php Operators
If Normal, alternate, short switch while do-while for continue break foreach return require Include d1controlstruct.php source d1controlstruct.php Control structures
conditional If (expression) true_statement; else false_statement; if (expression1) { } elseif (expression2) { // Note that the space between else and if is optional } else { } alternate syntax opening brace { = : closing brace } = endif; if (expression1): statements; elseif (expression2): statements; endif; Ternary conditional operator $variable = (expression) ? true_expression: false_expression; if statement