1 / 17

PHP

PHP. Basic Scripting & Variables. Yasar Hussain Malik - NISTE. Whitespace Escape sequences Brief introduction to variable types Data Types Variable Functions Variable variables Pre-set variables References (pointer) Constants Mathematical Constants Operators

steviea
Download Presentation

PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Basic Scripting & Variables Yasar Hussain Malik - NISTE

  2. Whitespace • Escape sequences • Brief introduction to variable types • Data Types • Variable Functions • Variable variables • Pre-set variables • References (pointer) • Constants • Mathematical Constants • Operators • The Ternary Operator • Operator precedence and associability • Variable Scope

  3. Whitespace • Spaces, tabs, and new lines in between statements have no effect on how the code is executed. For example, this next script is the same as the previous script, even though it is laid out quite different: • <?php    $name = "Paul"; print "Your name is $name\n";    $name2 = $name; $age = 20;    print "Your name is $name2, and your age is $age\n";    print 'Goodbye, $name!\n';?> • It is generally recommended that you use whitespace to separate your code into clear blocks, so that it can almost be understood simply by visually inspecting the layout. like • $name = "Paul"; • print "Your name is $name\n";$name2 = $name; • $age = 20;

  4. Escape Sequences • Escape sequences consist of a backslash followed by a single character. When enclosed in double quotes, the backslash causes the interpretation of the next character to "escape" from its normal ASCII code and to represent something else. To display the escape sequences in your browser, the HTML <pre> tag can be used; otherwise, the escape sequences placed within your PHP script will not be interpreted. • \‘ Single quotation mark • \“ Double quotation • \t Tab • \n Newline • \r Return/line feed • \$ A literal dollar sign • \\ Backslash • \70 Represents the octal value • \x05 Represents the hexadecimal character

  5. Data Types • PHP supports four core data types: • Integer • Float (also called double) • String • Boolean • In addition to the four core data types, there are four other special • types: • Null • Array • Object • Resources • To see the type of a variable use function $type=gettype(54.6);

  6. String Literals and Quoting • String literals are a row of characters enclosed in either double or single quotes.1The quotes must be matched. • If a string of characters is enclosed in single quotes, the characters are treated literally (each of the characters represents itself). • Double quotes do not treat all characters equally. If a string is enclosed in double quotes, most of the characters represent themselves, but dollar signs and backslashes have a special meaning • Single quotes can hide double quotes, and double quotes can hide single quotes • "This is a string“ • 'This is another string‘ • "This is also 'a string‘“ • 'This is "a string"‘ • "5" is a string, whereas 5 is a number.

  7. Boolean Literals • Boolean literals (introduced in PHP 4) are logical values that have only one of two values, true or false, both case insensitive. • You can think of the values as yes or no, on or off, or 1 or 0. • When using numeric comparison and equality operators, the value true evaluates to 1 and false evaluates to the empty

  8. Special Data Types • Null NULL represents "no value," meaning "nothing," not even an empty string or zero. It is a type of NULL. An uninitialized variable contains the value NULL. A variable can be assigned the value NULL, and if a variable has been unset, it is considered to be NULL. • Resource A resource is a special variable, holding a reference to an external resource such as a database object or file handler. Resources are created and used by special functions. File and database resources are defined by the PHP interpreter and are only accessible by functions provided by the interpreter. • The gettype() Function The gettype() built-in function returns a string to identify the data type of its argument. The argument might be a variable, string, keyword, and so on. You can use the gettype() function to check whether or not a variable has been defined because if there is no value associated with the variable, the gettype() function returns NULL Format string gettype ( mixed var ) $type=gettype(54.6); // Returns "float" (e)

  9. Variables • To test whether a variable has a value set simply call the isset() function passing through the name of the variable as an argument . The following code example tests to see if a variable has a value or not and displays a message accordingly: • <?php • $myVariable = "hello"; • if (isset($myVariable)) • { • echo "It is set."; • } • else • { • echo "It is not set."; • } • ?>

  10. …Variables Casting • Forcing a type with type casting • PHP will automatically convert data types as necessary across the board . • If you specifically wish to override PHP's type conversion, you can perform what is called a type cast • you forcibly convert a variable of type A to type B. In PHP, type casting looks like this: • <?php    $mystring = "wombat";    $myinteger = (integer)$mystring?>

  11. Variable Scope When you declare a variable outside a function, it may not be seen in the function. When you declare a variable inside a function, it may not be seen outside the function. That feature is called variable scope.

  12. Constants If you find yourself setting a variable for convenience and never changing it during a script, chances are you should be using a constant. Constants are like variables except that once they are defined, they cannot be undefined or changed they are constant, as the name suggests. Unlike many other languages, constants are not faster than variables in PHP. The primary advantage to using constants is the fact that they do not have a dollar sign at the front and, therefore, are visibly different from variables. Furthermore, constants are automatically global across your entire script, unlike variables.

  13. …Constants • You can define a constant by using the define()-function or by using the const keyword outside a class definition as of PHP 5.3.0. Once a constant is defined, it can never be changed or undefined. • Only scalar data (Boolean , integer, float and string) can be contained in constants. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results. • You can get the value of a constant by simply specifying its name. Unlike with variables, a constant should not have a$ sign. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically. Use get_defined_constants() to get a list of all defined constants

  14. …Constants • These are the differences between constants and variables: • Constants do not have a dollar sign ($) before them; • Constants may only be defined using the define() function, not by simple assignment; • Constants may be defined and accessed anywhere without regard to variable scoping rules; • Constants may not be redefined or undefined once they have been set; and • Constants may only evaluate to scalar values. • Example Defining Constants • <?php define("CONSTANT", "Hello world."); echo CONSTANT; // outputs "Hello world." echo Constant; // outputs "Constant" and issues a notice.// Defining Constants using the const keyword const CONSTANT = 'Hello World'; echo CONSTANT; ?>

  15. Operators

  16. …Operators

  17. The Ternary Operator • It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false. The ternary operator is a shorthand way of doing if statements. Here's an example: • <?php    $agestr = ($age < 16) ? 'child' : 'adult';?>

More Related