Thursday, August 23, 2018

Videos for Week 3 Chapters 3-4

Getting started with the MAMP/WAMP server

https://www.youtube.com/watch?v=Qy2F7qpy46k


Textbook Chapter 1: Client-Server Interactions

https://www.youtube.com/watch?v=2hIOOP0ztEo


Chapter 3 Part 1

https://www.youtube.com/watch?v=Xtw3cXM93Xk

Code comments, PHP source code vs. HTML source code vs. HTML visual, Arithmetic operators, increment, automatic conversions, arrays, print_r, array indexing, out of bounds, variable naming rules.


<!--
Susan S. Student
9 Sept. 2017
PHP 86 at Mission
-->

<?php

  /* Susan S. Student
     9 Sept. 2017
     PHP class CIS 086 at Mission College
   */

  // This is a comment
  // echo "X equals $x";

  $x = 50;
  echo $x;
  $x += 14;
  echo "<br />\n";
  echo $x;
  $x = $x + 14;
  echo "<br />\n";
  echo $x;
  $x++;
  echo "<br />\n";
  echo $x;
  $x--;
  echo "<br />\n";
  echo $x;

  $x = "Fred Smith";
  echo "<br />\n";
  echo $x;

  $x = 3.14;
  echo "<br />\n";
  echo $x;

  $x = array ("Alice", "Bob", "Chris", "Dean", "Eliza");
  echo "<br />\n";
  print_r ($x);

  $x = [ "Red", "Orange", "Yellow", "Green", "Blue", "Purple" ];
  echo "<br />\n";
  print_r ($x);

  echo "<br />\n";
  echo $x[3];

  echo "<br />\n";
  echo $x[5];

  echo "<br />\n";
  echo $x[2];

  echo "<br />\n";
  echo $x[4];

  echo "<br />\n";
  echo $x[0];

  echo "<br />\n";
  echo $x[1];
  echo "<br />\n";

  $_foo = "bar";
  echo $_foo;
  echo "<br />\n";

  /*$1foo = "no bar";
  echo $1foo;
  echo "<br />\n";*/

  /*foo = "wassap";
  echo foo;
  echo "<br />\n";*/

  echo $_FOO;
  echo "<br />\n";
?>

Chapter 3 Part 2

https://www.youtube.com/watch?v=3PkEEjhzpAI

Arithmetic, integers, floating point, modulo operator, comparison operators, logical operators (and, or)


<?php

  $a = 50; 
  $b = 6;
  $d = 14;
  $e = 14;
  
  // arithmetic operators
  $c = $a + $b;
  echo "$c<br />\n";

  $c = $a - $b;
  echo "$c<br />\n";

  $c = $a * $b;
  echo "$c<br />\n";

  $c = $a / $b;
  echo "$c<br />\n";

  $c = $a % $b;
  echo "$c<br />\n";

  // arithmetic assignment operators
  $c += 17;
  echo "$c<br />\n";

  $c -= 37;
  echo "$c<br />\n";

  $c *= -2;
  echo "$c<br />\n";

  $c /= 3;  // $c = $c / 3;
  echo "$c<br />\n";

  // comparison operators
  if ($a == $b)
    echo "Equal<br />\n";
  else
    echo "Not Equal<br />\n";

  if ($a != $b)
    echo "Not Equal<br />\n";
  else
    echo "Not Not Equal<br />\n";

  if ($a < $b) 
      echo "A is less than B<br />\n";
  else
      echo "A is NOT less than B<br />\n";

  if ($a > $b) 
      echo "A is greater than B<br />\n";
  else
      echo "A is NOT greater than B<br />\n";

  // logical operators: AND
  if ($a < $b && $b < $d)
    echo "The numbers are in order<br />\n";
  else
    echo "The numbers are NOT in order<br />\n";

  // logical operators: AND
  if ($a > $b && $a > $d) 
    echo "A is the bigger number<br />\n";
  else
    echo "A is NOT the bigger number<br />\n";

  // logical operators: OR
  if ($b == $d || $d == $e)
    echo "Two of the numbers are the same<br />\n";
  else
    echo "No two of the numbers are the same<br />\n";
?>

Chapter 3 Part 3

https://www.youtube.com/watch?v=F4WPxwkmaWY

Strings, concatenation, multi-line statements, automatic conversions, constants, define, substr (substring)


<?php

  $msgs = 417;
  $s = "You have " . $msgs . " messages.<br />\n";
  echo $s;
  $t = "You have $msgs messages.<br />\n";
  echo $t;

  $t .= "Please listen to your messages.<br />\n";
  echo $t;

  // $t .= 'The machine is almost out of space.<br />\n';
  $t .= 'Ray\'s auto parts called.<br />';
  echo $t;

  $p = "She wrote upon it, \"Return to sender.\"";
  echo $p;
  echo "<br />\n";

  $p = "She wrote upon it, 'Return to sender.'";
  echo $p;
  echo "<br />\n";

  $p = 'She wrote upon it, "Return to sender."';
  echo $p;
  echo "<br />\n";

  $lincoln = "Fourscore and seven years ago our forefathers " .
      "brought forth on this continent a new nation<br />\n";

  echo $lincoln;

  $number = 12345 * 67890;
  echo $number . "<br />\n";
  echo substr ($number, 3, 3);
  echo "<br />\n";
  echo substr (1234, 1, 2);
  echo "<br />\n";
  echo substr (1234, 1, 2) * 2;
  echo "<br />\n";

  define ("PI", 3.1415926);
  $radius = 6;
  echo $radius * $radius * PI;
  echo "<br />\n";

  // constants
  define ("STUDENT_PRICE", 9.50);
  define ("SENIOR_PRICE", 8.00);
  define ("CHILD_PRICE", 6.00);

  $student_price = 9.50; // NOT a constant because it can be changed later

  $student_price = 10.50; // ok to change a variable value

  // STUDENT_PRICE = 10.50;
  define ("STUDENT_PRICE", 10.50); // will not happen
  echo STUDENT_PRICE;
?>


Chapter 3 Part 4

https://www.youtube.com/watch?v=kYMS30XNmZE

Functions, return values, scope, local scope, global scope, superglobals ($_SERVER)



<?php

function printline ($s) {
    echo $s . "<br />\n";
}

printline (3+4);
$x = 3;
$m = 5;
$b = 7;
$y = $m * $x + $b;
printline ($y);
printline ("Now is the time for all good men ...");

function calculate_y ($x, $m, $b) {
    return ($m * $x + $b);
}

printline (calculate_y ($x, $m, $b));

$y = calculate_y ($x, $m, $b);
printline ($y);

printline (calculate_y( 5, 2, 9));
printline (calculate_y( 9, 42, 17));

// global variable
$rsquared = 16;

function calculate_area ($r) {
    $rsquared = $r * $r; // local variable
    return 3.1415926 * $rsquared;
}

$a = calculate_area (5);
printline ($a);

printline ($rsquared);

function calculate_volume ($r) {
    $rsquared = $r * $r; // local variable
    $rcubed = $rsquared * $r;
    return $rcubed * 3.1415926 * 4 / 3;
}

printline (calculate_volume (5));

// $i, $j, $a, $b, $x, $y

print_r ($_SERVER);
print_r ($_GET);
print_r ($_POST);
print_r ($_COOKIE);

echo `ls -al`;
echo `du -sk *`;
echo `ls -al ../..`;
?>

Chapter 4 Part 1

https://www.youtube.com/watch?v=X4E7dbsXzKo

Boolean logic, and, or, not, literals, variables, quotation marks in strings, unary operator, binary operator, operator precedence, parentheses, operator associativity.


<?php

// literals
$name = "Mark"; // Mark is a literal; $name is a variable
$age = 39; // lying
$students = array ("George", "Wilma", "Ivan", "Rose");
$stuff = array ("Tomatoes", 7, "Milk", 3.14);

// $name = Mark Brautigam; // no good, string must be in quotation marks
echo $name; // printing a variable
echo $age;

echo "Mary"; // print a literal

print_r ($students);
print_r ($stuff);
print_r ($students + $stuff);

// unary operator has one operand
// = is a binary operator, has two operands
$a = - $b;

// precedence
$a = 3 + 5 * 7; // multiply first
                // addition 2nd
                // = assignment LAST

$a = 3 * 5 + 7; // multiply, then add, then assign

// change OR CLARIFY precedence with parentheses
$a = (3 + 5) * 7; // parentheses first, then +, then * , then =

// example

total = 1 + 2 * 3 / 4 + 5 * 6 - 7 / 8 + 9
total = 1 + (2 * 3) / 4 + 5 * 6 - 7 / 8 + 9
total = 1 + (6 / 4) + (5 * 6) - (7 / 8) + 9
total = (1 + 1.5) + 30 - 0.875 + 9
total = 2.5 + 30 - 0.875 + 9
total = 32.5 - 0.875 + 9
total = 31.625 + 9
total = 40.625

// [1] multiplication and division and modulo, left to right, no distinction
// [2] addition and subtraction, left to right, no distinction
// [3] assignment, right to left
?>




No comments:

Post a Comment