PHP Scripts

Working with the strings:


<html>
<head>
<title>Beginning PHP5</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<table width="100%" border="1">
  <tr>
    <td width="49%"><font face="Arial, Helvetica, sans-serif"><b>Working With
      Strings</b></font></td>
    <td width="51%">&nbsp;</td>
  </tr>
  <tr>
    <td width="49%"><font face="Arial, Helvetica, sans-serif" size="-1">Using
      Concatenation - the . operator</font></td>
    <td width="51%"><font face="Arial, Helvetica, sans-serif" size="-1"> <?php
$first_name = "Joe";
$last_name = "Blow";
$whole_name = $first_name . " " . $last_name;
echo "First name plus last name = <b>$whole_name</b>";
?> </font></td>
  </tr>
  <tr>
    <td width="49%"><font face="Arial, Helvetica, sans-serif" size="-1">Finding
      String Length - using <b>strlen()</b></font></td>
    <td width="51%"><font face="Arial, Helvetica, sans-serif" size="-1"> <?php
$string_length = strlen($whole_name);
echo "The length of the name is <b>" . $string_length . "</b>";
?> </font></td>
  </tr>
  <tr>
    <td width="49%"><font face="Arial, Helvetica, sans-serif" size="-1">Getting
      Part of a String - using <b>strstr()</b></font></td>
    <td width="51%"><font face="Arial, Helvetica, sans-serif" size="-1"> <?php
$part_after_space = strstr($whole_name, " ");
echo "The part of the string after the space is <b>" . $part_after_space . "</b>";
?> </font></td>
  </tr>
  <tr>
    <td width="49%"><font face="Arial, Helvetica, sans-serif" size="-1">Finding
      Position of Part of a String - using <b>strpos()</b></font></td>
    <td width="51%"><font face="Arial, Helvetica, sans-serif" size="-1"> <?php
$letter_position = strpos($whole_name, "o");
echo "The position of the letter &quot;a&quot; is <b>" . $letter_position . "</b>";
?> </font></td>
  </tr>
  <tr>
    <td width="49%"><font face="Arial, Helvetica, sans-serif" size="-1">Return
      a character based on an ASCII value - using <b>chr()</b></font></td>
    <td width="51%"><font face="Arial, Helvetica, sans-serif" size="-1"> <?php
$ascii_character_returned = chr(97);
echo "The character corresponding to ASCII decimal value 97 is <b>" . $ascii_character_returned . "</b>";
?> </font></td>
  </tr>
</table>
</body>
</html>

Checkboxes:
<html>
<head><title></title></head>
<body>
<?php
echo $_POST['Choice'];
?>
</body>
</html>

PHP cookies:
<?php
//cookies.php
if (!empty($_POST['type_sel'])) {
$type = $_POST['type_sel'];

   setcookie("Type", $type, time()+3600);
}

if (!empty($_POST['size_sel'])) {
$size = $_POST['size_sel'];
   setcookie("Size", $size, time()+3600);
}

//We define some options for font size and typeface, and as it's now safe to add an //HTML header, we do so:
$type = array("arial", "helvetica", "sans-serif", "courier");
$size = array("1","2","3","4","5","6","7");
echo "<html><head><title>Cookie Test</title></head><body><div align='center'>";

//The following form contains a pair of listboxes, which can be used to specify the //user's preferences:
echo "<form method='POST'>";
echo "What font type would you like to use? ";
echo "<select name='type_sel'>";
echo "<option selected value=''>default</option>";
foreach ($type as $var) {
   echo "<option>$var</option>";
}
echo "</select><br><br>";
echo "What font size would you like to use? ";
echo "<select name='size_sel'>";
echo "<option selected value=''>default</option>";

foreach ($size as $var) {
   echo "<option>$var</option>";
}
echo "</select><br><br>";
echo "<input type='submit'>";
echo "</form>";

//Finally, we echo out some useful information, and format it using appropriate //settings:
echo "<b>Your cookies say:</b><br>";
echo "<font ";
if (isset($_COOKIE['Type'])) {
echo "face='$_COOKIE[Type]' ";
}

if (isset($_COOKIE['Size'])) {
 echo "size='$_COOKIE[Size]' ";
}
echo ">";
if (isset($_COOKIE['Type']))
echo "Type = $_COOKIE[Type]";
echo"<br>";
if (isset($_COOKIE['Size']))
echo "Size = $_COOKIE[Size]";
echo "</font><br>";

echo "<b>Your form variables say:</b><br>";
echo "<font ";

if (isset($_POST['type_sel'])) {
 echo "face='$_POST[type_sel]' ";
}

if (isset($_POST['size_sel'])) {
 echo "size='$_POST[size_sel]' ";
}
echo ">";
if (isset($_POST['type_sel']))
echo "Type = $_POST[type_sel]<br>";
if (isset($_POST['size_sel']))
echo "Size = $_POST[size_sel]<br>";
echo "</font>";
echo "</div></body></html>";
?>


Listboxes:
<html>
<head><title></title></head>
<body>
<?php
echo "Price Range: $_POST[Price]";
$Choice0 = $_POST['EngineSize'][0];
$Choice1 = $_POST['EngineSize'][1];
$Choice2 = $_POST['EngineSize'][2];
$Choice3 = $_POST['EngineSize'][3];
echo "<br>Engine Size(s): $Choice0";
echo "$Choice1";
echo "$Choice2";
echo "$Choice3";
?>
</body>
</html>


PHP Sessions:
<?php

session_register('view1count');
session_register('view2count');
session_register('view3count');
session_register('view4count');
?>
<?php

//The rest of the script illustrates how to make hyperlinks that hand PHP what it needs to access your session data - namely, SID.
echo "<html><head><title>Web Page Hit Counter</title></head><body>";
if (isset($_GET['whichpage'])) {
   echo "<b>You are currently on page $_GET[whichpage].</b><br><br>\n";
  $_SESSION["view".$_GET['whichpage']."count"]++;
}

for ($i = 1; $i <= 4; $i++) {
   if (isset($_GET['whichpage']) == $i) {
      echo "<b><a href=\"sessions.php?".session_id()."&whichpage=$i\">Page $i</a></b>";
   } else {
      echo "<a href=\"sessions.php?".session_id()."&whichpage=$i\">Page $i</a>";
   }
if (!isset($_SESSION["view".$i."count"])) $_SESSION["view".$i."count"] = 0;
   echo ", which you have chosen ".$_SESSION["view".$i."count"]." times.<BR>\n";
}
echo "\n\n<br><br>\n\n";
echo "</body></html>";

?>


PHP arrays:
<?php
$states_of_the_USA = array (1 => "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
for ($counter=1; $counter<51; $counter++) {
   echo"<BR>$states_of_the_USA[$counter]"; 
}

?>



PHP capitals:
<html>
<head><title></title></head>
<body>
<?php
if (isset($_POST['posted'])) {
$state_capital = array (0 => "Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento","Denver","Hartford", "Dover","Tallahasse", "Atlanta", "Honolulu", "Boise", "Springfield","Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge","Augusta","Annapolis","Boston", "Lansing", "Saint Paul","Jackson", "Jefferson City", "Helena","Lincoln", "Carson City","Concord", "Trenton","Santa Fe", "Albany", "Raleigh","Bismarck","Columbus","Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia","Pierre", "Nashville", "Austin","Salt Lake City", "Montpelier","Richmond","Olympia","Charleston", "Madison","Cheyenne");
for ($counter=0; $counter<50; $counter++) 
{
  if($_POST['hiddenstate'][$counter]==$_POST['state']) 
  {
     echo"The state capital of $_POST[state] is <b>$state_capital[$counter]</b><hr>";
  }
}
}
?>
<form action="capitals.php" method="POST">
<input type="hidden" name="posted" value="true">
What state do you want to know the capital of?
<select name="state">
<?php
$states_of_the_USA = array (1 => "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
for ($counter = 1; $counter < 51; $counter ++) {
   echo"<option>$states_of_the_USA[$counter]</option>"; 
}
echo "</select><br><br>";
for ($counter = 1; $counter < 51; $counter++) {
   echo"<input type='hidden' name='hiddenstate[]' value='$states_of_the_USA[$counter]'>";
}
?>
<input type="submit" value="Find Capital">
</form>
</body>
</html> 


PHP cars:
<html>
<head><title></title></head>
<body>
<b>Namllu car hire company</b>
<?php
if (isset($_POST['posted'])) { 

if ($_POST['age'] > 20 and $_POST['license'] == "on") {
echo ("Your car hire has been accepted.<hr>");
}

if ($_POST['age'] < 21 or $_POST['license'] == "") {
echo ("Unfortunately we cannot hire a car to you.<hr>");
}
}
?>
<form method="post" action="car.php">
<input type="hidden" name="posted" value="true">
First name:
<input name="first_name" type="text">
Last name:
<input name="last_name" type="text">
age:
<input name="age" type="text"size="3">
<br>
<br>
Address:
<textarea name="address" rows=4 cols=40>
</textarea>
<br>
<br>
Do you hold a current driving license?
<input name="license" type="checkbox">
<br>
<br> 
<input type="submit" value="Submit application">
</form>
</body>
</html>



PHP foreach:
<html>
<head><title></title></head>
<body>
<?
$states_of_the_USA = array ("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
foreach($states_of_the_USA as $state_index => $state)
{
   echo "<br>$state_index - $state";
}
?>
</body>
</html>




PHP Quiz:
<html>
<head><title></title></head>
<body>
<?php
if (isset($_POST['posted'])) { 
if ($_POST['question1'] == "Lisbon") {
echo "You are correct, $_POST[question1] is the right answer<hr>";
}

if ($_POST['question1'] != "Lisbon") {
echo "You are incorrect, $_POST[question1] is not the right answer<hr>";
}
}
?>
<form method="POST" action="quiz.php">
<input type="hidden" name="posted" value="true">
What is the capital of Portugal?
<br>
<br>
<input name="question1" type="radio" value="Porto">
Porto
<br>
<input name="question1" type="radio" value="Lisbon">
Lisbon
<br>
<input name="question1" type="radio" value="Madrid">
Madrid
<br>
<br>
<input type="submit">
</form>
</body>
</html>


Next Scripts:

No comments:

Post a Comment