Question? Leave a message!




Basic PHP

Basic PHP
Dr.ShaneMatts Profile Pic
Dr.ShaneMatts,United States,Teacher
Published Date:23-07-2017
Website URL
Comment
Using PHP – Part 2  THIS WEEK  Basic PHP The PHP web site  Detailed use of forms  Handling strings www.php.net  Regular expressions  Reading and writing files  Object oriented PHP  LAST WEEK  Adding PHP to a web page  Variables, constants, types  Printing strings and variables  What data is available to a PHP page?  How do pages know what a user has typed into a form? http://writecodeonline.com/php/  Write a web form with a text box  Users type PHP code into the text box  When they hit RUN, the page executes the code and shows the results  You can do this too This is some HTML code Escaping ?php echo "divThis bit is coded using PHP/div"; ? And this is HTML again ?php Choosing if (condition) which bit ? of HTML It was true. ?php to display else ? It was false. ?php ? ?php Comments can be // This is a comment. shown in 3 different This is also a comment. ways / This is a multi-line comment. / ?  Scalar types Types  boolean  integer  float (= double)  string  Compound types  array  object  Special types  resource, NULL  Two values, True and False  Case insensitive: True, TRUE, true are all the same.  The following are all considered to be false:  boolean FALSE  integer 0, float 0.0  empty string "", the string "0"  an empty array, and objects with no members  the special type NULL  Everything else is regarded as true  Integers  PHP doesn't support unsigned integers  The maximum values are platform dependent  Real numbers (various formats can be used)  1.234, 1.2e3, 7E-10  Strings  'not expanded', "expanded"  can also use HEREDOC format (see manual)  Arrays  a = array(value, …); a = array(key = value, …);  To refer to a value, either of these works ▪ datakey, datakey ▪ To see the contents of an array, use print_r(a);  Objects  You can define classes in PHP (see manual)  Resources  special variables holding references to external resources  these are used when extracting data from databases  NULL  variables with no value (the only value they can be assigned is also called NULL, null, Null, etc)  unset(a) makes a equal to NULL  Variables begin with a dollar sign Variables  The basic operations available are much the same as in Java, e.g. addition x = y + z;  Variables have the same type as their values  The type of a variable can change (type juggling) Assignments  x = 1;  myname = "Mike";  result = array("hi" = "there); Constants  Normal method  define("NAME", "Mike");  Version 5.3.0 onwards  const NAME = "Mike";  Can only take scalar values  if (cond) … elseif … else … Control  while (cond) … expressions  do … while (cond)  for (init; step; cond) …  break, continue  foreach (arr as val) …  foreach (arr as key = val) …  switch (val) case 'x': …; break;  default  Define functions using function hello() the function keyword echo "hello there :)"; hello();  Can define functions inside if statements if (x) function doStuff() …  Can even define functions inside other function hi() functions function there() … PHP 5 includes an function inverse(x) exception-style if (x) mechanism for throw new Exception( handling errors "Can't divide by zero" ); else return 1/x; try inverse(0); catch (Exception e) …  Suppose you are asked to write a very simple form-based web page  The user types their name and then submits the form  A new page opens, saying hello  For example, if I type "mike" in the box, the new page says "Hello mike"  If the box is left empty, the new page says "Hello stranger"  How do we do this? getname.php sayhello.php This page contains the form. This page is called by the form The user submits the form by when the use clicks the submit clicking a "submit" button. button. It displays the "hello" message. Later we'll see how to do both jobs using just a single page.