1 / 55

Server-Side Scripting with PHP

Server-Side Scripting with PHP. ISYS 475. PHP Manual Website. http://www.php.net/manual/en/. Hyper Text Transfer Protocol: Request & Response. HTTP Request. Browser. Web Server. HTTP Response. Web Application. Data Sent with Request and Response. Request:

dinah
Download Presentation

Server-Side Scripting with 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. Server-Side Scripting with PHP ISYS 475

  2. PHP Manual Website • http://www.php.net/manual/en/

  3. Hyper Text Transfer Protocol: Request & Response HTTP Request Browser Web Server HTTP Response Web Application

  4. Data Sent with Request and Response • Request: • requested URL, cookies, queryString, data from a form, etc. • Response: • Web page content in HTML code • Cookies • Etc.

  5. PHP Language Syntax

  6. PHP Variables • Variables in PHP are represented by a dollar sign followed by the name of the variable. • The variable name is case-sensitive. • Variable names can contain letters, numbers, and underscores.For examples: $num1, $_mySalary • Variable names can’t begin with a digit or two underscores. • Variables do not have intrinsic types – it depends on the first data assigned to it.

  7. Basic Data Types • Integers • Doubles: are floating-point numbers • Booleans: two possible values either true or false. • NULL: is a special type that only has one value: NULL. • Strings: are sequences of characters • Arrays: are named and indexed collections of other values. • Objects: are instances of programmer-defined classes • Resources: are special variables that hold references to resources external to PHP (such as database connections).

  8. Without using concatenation: $name="$first_name $last_name"; echo $name; echo "My name is $name "

  9. Note: echo and print have the same function.

  10. Math Functions:http://www.php.net/manual/en/ref.math.php Examples: pow ( number $base , number $exp) echo pow(2, 3);

  11. Difference between these functions: http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/

  12. PHP Superglobal variables • Superglobals are built-in variables that are always available in all scopes

  13. $_GET • An associative array of variables passed to the current script via the URL parameters (queryString).

  14. $_POST • An associative array of variables passed to the current script via the HTTP POST method.

  15. Example: Compute Sum <div> <form name="sumForm" method="post" action="computeSum.php"> <label>Value 1:</label> <input type="text" name="num1" value="" /><br><br> <label>Value 2:</label><input type="text" name="num2" value="" /><br><br> <label>Sum:</label> <input type="text" name="sum" value="" /><br><br> <input type="submit" value="Compute Sum" name="btnSubmit" /> </form> </div>

  16. Example: computeSum.php <body> <?php $num1=$_POST["num1"]; $num2=$_POST["num2"]; $sum=$num1+$num2; echo "<label>Value 1:</label> <input type='text' name='num1' value='$num1'/><br><br>"; echo "<label>Value 2:</label> <input type='text' name='num2' value='$num2' /><br><br>"; echo "<label>Sum:</label> <input type='text' name='sum' value='$sum' /><br><br>"; ?> </body>

  17. Embed PHP code in HTMLUsing PHP Expression:<?php?> <body> <?php $num1=$_POST["num1"]; $num2=$_POST["num2"]; $sum=$num1+$num2; ?> <label>Value 1:</label> <input type="text" name="num1" value="<?php echo $num1;?>" /><br><br> <label>Value 2:</label><input type="text" name="num2" value="<?php echo $num2;?>" /><br><br> <label>Sum:</label> <input type="text" name="sum" value="<?php echo $sum;?>" /><br><br> </body>

  18. Compute Future Value:Process form with various controls

  19. Form Code <form name="fvForm" method="post" action="computeFV.php"> Enter present value: <input type="text" name="PV" value="" /><br><br> Select interest rate: <select name="Rate"> <option value=.04>4%</option> <option value=.05>5%</option> <option value=.06>6%</option> <option value=.07>7%</option> <option value=.08>8%</option> </select><br><br> Select year: <br> <input type="radio" name="Year" value="10" />10-year<br> <input type="radio" name="Year" value="15" />15-year<br> <input type="radio" name="Year" value="30" />30-year<br><br> <input type="submit" value="ComputeFV" name="btnCompute" /> </form>

  20. PHP Code Example <?php $myPV=$_POST["PV"]; $myRate=$_POST["Rate"]; $myYear=$_POST["Year"]; $FV=$myPV*pow(1+$myRate,$myYear); echo "FutureValue is:" . $FV; ?>

  21. number_format — Format a number with grouped thousands • If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands. • If two parameters are given, number will be formatted with decimals with a dot (".") in front, and a comma (",") between every group of thousands.

  22. Examples: Comma and Currency format $number = 1234.56; $formated_number = number_format($number); // 1,235 $formated_number = number_format($number,2); // 1,235.56 $formated_number = “$” . number_format($number,2); // $1,235.56 echo "FutureValue is: $" . number_format($FV,2); NOTE: money_format() is undefined in Windows.

  23. Decision with If statement

  24. Note: { } is optional if only one statement

  25. PHP Loops

  26. Compute future value uses formula or a loop$FV = $myPV;for ($i = 1; $i <= $myYear; $i++) { $FV= $FV + $FV * $myRate; } <form name="fvForm" method="post" action="computeFVloop.php"> Enter present value: <input type="text" name="PV" value="" /><br><br> Select interest rate: <select name="Rate"> <option value=.04>4%</option> <option value=.05>5%</option> <option value=.06>6%</option> <option value=.07>7%</option> <option value=.08>8%</option> </select><br><br> Select year: <br> <input type="radio" name="Year" value="10" />10-year<br> <input type="radio" name="Year" value="20" />20-year<br> <input type="radio" name="Year" value="30" />30-year<br><br> Compute using loop:<input type="checkbox" name="chkLoop" value="ON" /><br><br> Future value is :<input type="text" name="FV" value="" /><br><br> <input type="submit" value="ComputeFV" name="btnCompute" /> </form>

  27. PHP Code <?php $myPV=$_POST["PV"]; $myRate=$_POST["Rate"]; $myYear=$_POST["Year"]; if (isset($_POST["chkLoop"])) { $FV = $myPV; for ($i = 1; $i <= $myYear; $i++) { $FV= $FV + $FV * $myRate; }} else $FV=$myPV*pow(1+$myRate,$myYear); echo "FutureValue is: $" . number_format($FV,2); ?> Note: Without checking isset($_POST["chkLoop"]) the IF statement will cause error if the checkbox is not checked.

  28. Depreciation Table Straight Line Depreciation Table <form name="depForm" method="post" action="createDepTable.php"> Enter Property Value: <input type="text" name="pValue" value="" /><br> Enter Property Life: <input type="text" name="pLife" value="" /><br> <input type="submit" value="Show Depreciation Table" name="btnShowTable" /> </form>

  29. Output

  30. <?php $value=$_POST["pValue"]; $life=$_POST["pLife"]; echo "Straight Line Depreciation Table <br>"; echo "Property Value: <input type='text' name='pValue' value='$value' /><br>"; echo "Property Life: <input type='text' name='pLife' value='$life' /><br>"; $depreciation=$value/$life; $totalDepreciation=$depreciation; echo "<table border='1' width='400' cellspacing=1>"; echo "<thead> <tr> <th>Year</th> <th>Value at BeginYr</th>"; echo "<th>Dep During Yr</th> <th>Total to EndOfYr</th></tr> </thead>"; echo "<tbody>"; for ($count = 1; $count <= $life; $count++) { echo "<tr>"; echo "<td width='25%'>$count</td>"; echo "<td width='25%'>$" . number_format($value,2) . "</td>"; echo "<td width='25%'>$" . number_format($depreciation,2) . "</td>"; echo "<td width='25%'>$" . number_format($totalDepreciation,2) . "</td>"; $value -= $depreciation; $totalDepreciation+=$depreciation; } ?>

  31. Alternating Row Color <?php $value=$_POST["pValue"]; $life=$_POST["pLife"]; echo "Straight Line Depreciation Table <br>"; echo "Property Value: <input type='text' name='pValue' value='$value' /><br>"; echo "Property Life: <input type='text' name='pLife' value='$life' /><br>"; $depreciation=$value/$life; $totalDepreciation=$depreciation; echo "<table border='1' width='400' cellspacing=1>"; echo "<thead> <tr> <th>Year</th> <th>Value at BeginYr</th>"; echo "<th>Dep During Yr</th> <th>Total to EndOfYr</th></tr> </thead>"; echo "<tbody>"; for ($count = 1; $count <= $life; $count++) { if ($count%2==0) $color="blue"; else $color="red"; // echo "<tr style='color:$color'>"; echo "<tr style='background-color:$color'>"; echo "<td width='25%'>$count</td>"; echo "<td width='25%'>$" . number_format($value,2) . "</td>"; echo "<td width='25%'>$" . number_format($depreciation,2) . "</td>"; echo "<td width='25%'>$" . number_format($totalDepreciation,2) . "</td>"; $value -= $depreciation; $totalDepreciation+=$depreciation; } ?>

  32. Using PHP Expression <?php $value=$_POST["pValue"]; $life=$_POST["pLife"]; echo "Straight Line Depreciation Table <br>"; echo "Property Value: <input type='text' name='pValue' value='$value' /><br>"; echo "Property Life: <input type='text' name='pLife' value='$life' /><br>"; $depreciation=$value/$life; $totalDepreciation=$depreciation; ?> <table border='1' width='400' cellspacing=1> <thead> <tr> <th>Year</th> <th>Value at BeginYr</th> <th>Dep During Yr</th> <th>Total to EndOfYr</th></tr> </thead> <tbody> <?php for ($count = 1; $count <= $life; $count++) { ?> <tr> <td width='25%'><?php echo $count;?></td> <td width='25%'><?php echo '$'. number_format($value,2)?></td> <td width='25%'><?php echo '$'. number_format($depreciation,2)?></td> <td width='25%'><?php echo '$'. number_format($totalDepreciation,2)?></td> </tr> <?php $value -= $depreciation; $totalDepreciation+=$depreciation; } ?> </tbody></table>

  33. Display output with inputs on the same page:Example: sum of two numbers • Method 1: • The page should be a php file, not a html page. • The page calls itself to process the data. • 2. the php file specified by the action attribute is called to compute the sum and use the include statement to display the sum with the input page

  34. Method 1: The PHP page, SumPage.php, calls itself <?php if (!empty($_POST)) //if (count($_POST)>0) { $num1=$_POST["num1"]; $num2=$_POST["num2"]; $sum=$num1+$num2; ?> <form name="sumForm" method="post" action="SumPage.php"> Value 1: <input type="text" name=num1 value="<?php echo $num1;?>" /><br><br> Value 2: <input type="text" name="num2" value="<?php echo $num2;?>" /><br><br> Sum: <input type="text" name="sum" value="<?php echo $sum;?>" /><br><br> <input type="submit" value="Compute Sum" name="btnSubmit" /> </form> <?php } else { ?> <form name="sumForm" method="post" action="SumPage.php"> Value 1: <input type="text" name="num1" value="" /><br><br> Value 2: <input type="text" name="num2" value="" /><br><br> Sum: <input type="text" name="sum" value="" /><br><br> <input type="submit" value="Compute Sum" name="btnSubmit" /> </form> <?php } ?> Note: This program uses empty($_POST) to test if the page is loaded for the first time.

  35. Display output with inputs on the same page:Example: sum of two numbers • Method 2: • The page should be a php file, not a html page. • The page calls a PHP page to process the data which uses an include statement to include the calling page for output

  36. PHP include and require Statements:include 'filename';require 'filename'; • In PHP, you can insert the content of one PHP file into another PHP file. The include and require statements are used to insert useful codes written in other files, in the flow of execution. • Include and require are identical, except upon failure: • require will produce a fatal error (E_COMPILE_ERROR) and stop the script • include will only produce a warning (E_WARNING) and the script will continue

More Related