02 Php Vars Op Control Etc

1,163 views

Published on

Programming Basics in PHP

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,163
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Assign $global and $GLOBALS
  • 02 Php Vars Op Control Etc

    1. 1. Day 2 PHP <ul><li>Geshan Manandhar </li></ul><ul><li>Developer, </li></ul><ul><li>Young Innovations Pvt. Limited </li></ul><ul><li>www.geshanmanandhar.com </li></ul>http://www.php.net
    2. 2. Variables in PHP <ul><li>Boolean: It can be either TRUE or FALSE . </li></ul><ul><li>Integer: The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value. </li></ul><ul><li>Float: The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value </li></ul>
    3. 3. Variable in PHP <ul><li>String: A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. </li></ul><ul><li>Array: An array in PHP is actually an ordered map. A map is a type that maps values to keys . </li></ul><ul><ul><li>$names[1] = “Ram”; </li></ul></ul><ul><ul><li>$names[2] = “Shyam”; </li></ul></ul>
    4. 4. Variables in PHP <ul><li>Objects: mainly have attributes and functions. </li></ul><ul><li><?php </li></ul><ul><ul><li>class test </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>function do_test() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>echo &quot;Testing class function.&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>$an_object = new test; </li></ul></ul><ul><ul><li>$an_object->do_test(); </li></ul></ul><ul><li>?> </li></ul>
    5. 5. Naming variables in PHP <ul><li>Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. </li></ul><ul><li>A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. </li></ul><ul><ul><li>$a_5=10; //valid </li></ul></ul><ul><ul><li>$#b = 20; //invalid </li></ul></ul><ul><li>Predefined Variables ($_SERVER, $_POST) </li></ul>
    6. 6. Constant <ul><li>You can define a constant by using the define() -function. Once a constant is defined, it can never be changed or undefined. </li></ul><ul><li><?php </li></ul><ul><li>define(&quot;PI&quot;, 3.1415); //defining a constant called PI </li></ul><ul><li>$r=5; //r for radius </li></ul><ul><li>$area = PI*$r*$r; //area calculation of circle </li></ul><ul><li>print &quot;Areas of circle with radius $r is &quot;.$area.&quot;.&quot;; </li></ul><ul><li>//user number_format() yourself. </li></ul><ul><li>?> </li></ul>
    7. 7. Arithmetic Operators See Program for more clarification: Day02prog06_Operators.php
    8. 8. Comparison Operators See code of Day02prog08_comparision_operators.php
    9. 9. Increment Decrement Operators <ul><li>++ and -- </li></ul>Code at Day02prog09_inc_dec_operator.php
    10. 10. Logical Operator
    11. 11. String Operator <ul><li>. used to concatenate strings </li></ul><ul><li>.= used to append a string. </li></ul><ul><li><?php $b = ”Hello” . &quot;World!&quot;; // now $b contains &quot;Hello World!&quot; $a = &quot;Hello &quot;; $a .= &quot;World!&quot;;  // now $a contains &quot;Hello World!&quot; ?> </li></ul>
    12. 12. If-else if- else <ul><li><?php </li></ul><ul><li>$a=10; </li></ul><ul><li>$b=17; </li></ul><ul><li>if ($a > $b) { </li></ul><ul><li>echo &quot;a is bigger than b&quot;; </li></ul><ul><li>} elseif ($a == $b) { </li></ul><ul><li>echo &quot;a is equal to b&quot;; </li></ul><ul><li>} else { </li></ul><ul><li>echo &quot;a is smaller than b&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
    13. 13. If-else if- else alternate <ul><li><?php </li></ul><ul><li>$a=10; </li></ul><ul><li>$b=17; </li></ul><ul><li>if ($a > $b) : </li></ul><ul><li>echo &quot;a is bigger than b&quot;; </li></ul><ul><li>elseif ($a == $b) : </li></ul><ul><li>echo &quot;a is equal to b&quot;; </li></ul><ul><li>else : </li></ul><ul><li>echo &quot;a is smaller than b&quot;; </li></ul><ul><li>endif; </li></ul><ul><li>?> </li></ul>
    14. 14. Switch Example <ul><li><?php </li></ul><ul><li>$engDay =date (&quot;l&quot;); </li></ul><ul><li>switch ($engDay) </li></ul><ul><li>{ </li></ul><ul><li>case &quot;Friday&quot;; </li></ul><ul><li>print &quot;Thank god its Friday&quot;; </li></ul><ul><li>break; </li></ul><ul><li>case &quot;Saturday&quot;; </li></ul><ul><li>print &quot;Oh its week end Saturday&quot;; </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>print &quot;Its just another working day of the week.&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
    15. 15. While Loop <ul><li><?php </li></ul><ul><li>$i = 1; </li></ul><ul><li>while ($i <= 10) { </li></ul><ul><li>echo $i++; </li></ul><ul><li>} </li></ul><ul><li>/* example 2 Alternative syntax*/ </li></ul><ul><li>$i = 1; </li></ul><ul><li>while ($i <= 10): </li></ul><ul><li>echo $i; </li></ul><ul><li>$i++; </li></ul><ul><li>endwhile; </li></ul><ul><li>?> </li></ul>
    16. 16. Do While Loop <ul><li><?php $i = 0; do {     echo $i; } while ($i > 0); ?> </li></ul>
    17. 17. For Loop <ul><li><?php </li></ul><ul><li>for ($i = 1; $i <= 10; $i++) { </li></ul><ul><li>echo $i; </li></ul><ul><li>} </li></ul><ul><li>/*Alternate for Syntax */ </li></ul><ul><li>for($i = 0; $i <= 10; $i++): </li></ul><ul><li>echo &quot;--&quot;.$i; </li></ul><ul><li>endfor; </li></ul><ul><li>?> </li></ul>
    18. 18. For Each loop <ul><li><?php </li></ul><ul><li>$arr = array(&quot;zero&quot; , &quot;one&quot; , &quot;two&quot; , &quot;three&quot; , &quot;four&quot; ); </li></ul><ul><li>/*For each example 1 */ </li></ul><ul><li>foreach ( $arr as $value ){ </li></ul><ul><li>print &quot;<br>&quot;.$value; </li></ul><ul><li>} </li></ul><ul><li>print &quot;<br>&quot;; </li></ul><ul><li>/*For each example 2 */ </li></ul><ul><li>foreach ( $arr as $key => $value) { </li></ul><ul><li>print &quot;<br>At key &quot;.$key.&quot; of the array, the value is &quot;.$value; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
    19. 19. PHP simple Function <ul><li><?php </li></ul><ul><li>function adder($var1, $var2){ </li></ul><ul><li>$sum = $var1+$var2; </li></ul><ul><li>return $sum; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Function with PHP an example</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><?php </li></ul><ul><li>$a=10; </li></ul><ul><li>$b=15; </li></ul><ul><li>$added = adder($a, $b); </li></ul><ul><li>?> </li></ul><ul><li>The sum of <?=$a?> and <?=$b?> is <?=$added?>. </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
    20. 20. Lets get rolling <ul><li>Write a program that performs all arithmetic operations with 3 variables. </li></ul><ul><li>Get the month with date(“F”) function and if its December and day date(“d”) is greater than 20 and less than 26, print “Merry Christmas”. </li></ul><ul><li>Print multiplication table of 5 with for loop. </li></ul><ul><li>Use foreach loop for an array called names and print the names in the array with its keys. </li></ul>
    21. 21. Questions are welcome <ul><li>That’s why god gave us two eyes, two ears but only one mouth. </li></ul>

    ×