Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. PHP: Hypertext Preprocessor<br />The Building Blocks<br />
  2. 2. Special Data Types<br />Resource - Reference to a third-party resource (a database, for example)<br />NULL - An uninitialized variable<br />Sample<br />&lt;?php<br /> $testing; // declare without assigning<br /> print gettype( $testing ); // NULL <br /> ?&gt;<br />
  3. 3. PHP’s Type Functions<br />var_dump()- tells you a variable&apos;s type and its contents<br />Sample<br />&lt;php<br />$testing = 5; <br />var_dump( $testing ); <br />?&gt;<br />
  4. 4. PHP’s Type Functions<br />gettype()function - to acquire the type of any variable. If you place a variable between the parentheses of the function call, gettype() returns a string representing the relevant type (NULL, integer, string, double , boolean) <br />Sample<br />&lt;?php<br />$testing = 5; <br />print gettype( $testing ); // integer <br />?&gt;<br />
  5. 5. PHP’s Type Functions<br />settype()function - to change the type of a variable. To use settype(), you must place the variable to change (and the type to change it to) between the parentheses and separate them by commas<br /> Sample<br /> &lt;?php<br /> $undecided = 3.14;<br />settype( $undecided, double );<br /> print gettype( $undecided ); <br /> print &quot; -- $undecided&lt;br /&gt;&quot;; // 3.14<br /> ?&gt;<br />
  6. 6. Operators and Expressions<br />Operators- are symbols that enable you to use one or more values to produce a new value. A value that is operated on by an operator is referred to as an operand.<br />4+3<br /> <br />Expression- is any combination of functions, values, and operators that resolves to a value.<br />4+3=9<br /> $user=you;<br />gettype( $user);<br /> <br />Assignment Operator - takes the value of its right operand and assigns it to its left operand<br />$name = &quot;matt&quot;;<br />
  7. 7. Operators and Expressions<br />Arithmetic Operators - The addition operator adds the right operand to the left operand, whereas the subtraction operator subtracts the right operand from the left. The division operator divides the left operand by the right, and the multiplication operator multiplies the left operand by the right. The modulus operator returns the remainder of the left operand divided by the right.<br />Example<br />(+)Addition (-)Subtraction (/)Division <br />(*) Multiplication (%)Modulus<br />
  8. 8. Concatenation Operator<br />The concatenation operator is a single period (.). Treating both operands as strings, it appends the right operand to the left<br />Sample<br />&quot;hello&quot;.&quot; world&quot; is equivalent to &quot;hello world&quot; <br />$centimeters = 212; <br />print &quot;the width is &quot;.($centimeters/100).&quot; meters&quot;; <br />
  9. 9. Combined Assignment Operator<br />consists of a standard operator symbol followed by an equals sign.<br />Example<br />$x = 4;$x = $x + 4; // $x now equals 8 <br /> can instead be written as<br /> $x = 4;$x += 4; // $x now equals 8<br />
  10. 10. Comparison Operators<br />perform tests on their operands. They return the boolean value true if the test is successful and return false otherwise. This type of expression is useful in control structures, such as if and while statements<br />Example<br />$x=5.1<br />$x &lt; 5<br />
  11. 11. Logical Operators<br />To test combinations of booleans<br />Sample<br />true || false = true<br /> true && false = false<br />
  12. 12. Operator Precedence<br />When you use an operator, the PHP engine usually reads your expression from left to right.<br />Sample<br />4 + 5=9<br /> 4 + 5 * 2 = 18 or 4 + 5 * 2 = 14 or (4 + 5) * 2 = 18<br />
  13. 13. Constants<br />Variables offer a flexible way of storing data because you can change their values and the type of data they store at any time. If, however, you want to work with a value that you do not want to alter throughout your script&apos;s execution, you can define a constant. You must use PHP&apos;s built-in function define() to create a constant. After you have done this, the constant cannot be changed. To use the define() function, you must place the name of the constant and the value you want to give it within the call&apos;s parentheses. These values must be separated by a comma, like so:<br />Example<br />define (&quot;CONSTANT_NAME&quot;, 42); <br />&lt;?php<br /> define (&quot;USER&quot;, &quot;Gerald&quot;); <br /> print &quot;Welcome&quot;.USER;<br /> ?&gt; <br />