{"id":95,"date":"2008-07-28T11:03:32","date_gmt":"2008-07-28T15:03:32","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=95"},"modified":"2008-07-28T11:10:00","modified_gmt":"2008-07-28T15:10:00","slug":"php-funpamentals","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=95","title":{"rendered":"PHP Funpamentals"},"content":{"rendered":"<p><strong>Basic syntax<\/strong><\/p>\n<p>Comments<br \/>\n\/\/ for single line<br \/>\n\/* *\/ for multiple line<br \/>\n# for shell style comments<\/p>\n<p><?php echo \"Hello World\"; ?><\/p>\n<p>Variables<br \/>\n-starts with $<br \/>\n-may contain<br \/>\n  strings<br \/>\n  numbers<br \/>\n  arrays<br \/>\n-is case sensitive<\/p>\n<p>Variable variables<br \/>\n  $foo = &#8216;bar&#8217;;<br \/>\n  $$foo = &#8216;baz&#8217;;<\/p>\n<p>Variable reference<br \/>\n  $black = &#038; $white<br \/>\n-unset a variable that is aliased<br \/>\n  unset($white);<br \/>\n  \/\/$black still = &#8220;snow&#8221;<\/p>\n<p>Global variables inside a fxn<br \/>\nfunction foo(){<br \/>\n  global $a;<br \/>\n  $a += 2;<br \/>\n}<\/p>\n<p>Static variable<\/p>\n<p>exit and return<br \/>\n-exit(&#8220;db fail&#8221;);<br \/>\n-die(&#8220;db fail&#8221;);<br \/>\n-$db = @mysql_connect(&#8220;localhost&#8221;, $USERNAME,$PASS)<br \/>\nor die(&#8220;db fail&#8221;);<\/p>\n<p>Constants<br \/>\n-define(&#8216;PUBLISHER&#8217;, &#8220;O&#8217;Reily&#8221;);<\/p>\n<p><strong>Strings<\/strong><\/p>\n<p>String concat<br \/>\n&#8216;therer were&#8217; . $n . &#8216; ducks.&#8217; ;<br \/>\nQuotes<br \/>\n  &#8216;some&#8217;<br \/>\n  &#8220;some&#8221;<\/p>\n<p>Here document<br \/>\n  $s = <<< END\n  Foo\n  END;\n\nPrint strings\n\n  echo \"hello\";\n  print(\"hello\") \/\/returns bool\n  printf()\n    '%.2f', 27.452 \/\/27.45\n    '%d %x', 214,214 \/\/214 d6\n    '%03d',7 \/\/007\n    '%02d\/%04y' \/\/15\/2004\n    '%.2f%%',2.1\/\/2.10%\n    '$%5.2f',4.1 \/\/$% 4.10\n  sprintf: save instead print\n  print_r()\n  var_dump\n\nString manipulation\n\nstrlen()  \ntrim()\/ltrim\/rtrim\nstrtolower()   strtoupper()\nucfirst() ucwords()\nString fxn fon html\nhtmlspecialchars()\nhtmlentities()\nstrip_tags()\nget_meta_tags()\nraw_url_encode()\nraw_url_decode()\nurl_encode()\nurl_decode()\nFor sql\nadd_slash()\n\nComparison\n$1 == $2\nstrcmp($1,$2); \/\/retuns -1,0,1\nstrcasecmp() \/\/conv to lower case\nstrnatcmp() \/\/natural order\nsoundex($str) \/\/sounds alike\nmetaphone($str)\nsimilar_text($str1,$str2[,pcnt]);\nlevenshtein($str1,$str2[,$cost_ins, $cost_rep,$cost_del]);\n\nManipulate string\nsubstr(string,start[, lengh]);\nsubstr_count(big_str, small_str);\nsubstr_replace(ori, new, start[, len]);\nstrrev($str); \/\/reverse string\nstr_repeat($str, count);\nstr_pad(topad,len[,with[,padtype]]};\n\nDecomp string\n$array=explode(sep,str[,limit]};\nimplode(sep,array); \/\/same join\n\nTokenizing\n$str=\"fred,flint,35,wilma\";\n$token=strtok($str,\",\");\nwhile($token != false){\n   echo (\"$token \");\n   $token = strtok(\",\");\n}\n\/\/ fred flint 35 wilma\nsscanf(str,temp[, var1,var2...]);\n\nString search\nstrpos(large_str, small_str);\nstrrpos(large_str, small_str);\nstrstr(large_str, small_str);\n\nDecompose url\n$array=parse_url(url);\n\n<strong>Arrays<\/strong><br \/>\n-define<br \/>\n  $person[0] = &#8216;Edison&#8217;;<br \/>\n  $person[&#8216;1&#8217;] = &#8216;Wankel&#8217;;<br \/>\n  $person = array(<br \/>\n    &#8216;Edison&#8217;,&#8217;Wankel&#8217;);<br \/>\n  $person = array(<br \/>\n    &#8216;0&#8217; => &#8216;Edison&#8217;,<br \/>\n    &#8216;1&#8217; => &#8216;Winkel&#8217;);<br \/>\n-use<br \/>\n  foreach ($person as $name){<br \/>\n    echo &#8220;Hello, $name\\n&#8221;;<br \/>\n  }<\/p>\n<p><strong>Class<\/strong><\/p>\n<p>Define Class<br \/>\nclass Person {<br \/>\n  var $name = &#8221;;<br \/>\n  function name($new = null) {<br \/>\n    if (! is_null($new)){<br \/>\n      $this->name = $new;<br \/>\n    }<br \/>\n  return $this->name;<br \/>\n  }<br \/>\n}<\/p>\n<p>Use Class<br \/>\n$ed = new Person;<br \/>\n$ed->name(&#8216;Edison&#8217;);<br \/>\nprintf(&#8220;Hello, %s\\n&#8221;,$ed->name;<\/p>\n<p><strong>Operators<\/strong><br \/>\n-Arithmetic<br \/>\n  +<br \/>\n  &#8211;<br \/>\n  *<br \/>\n  \/<br \/>\n  %<br \/>\n  ++<br \/>\n  &#8212;<br \/>\n-Assignment<br \/>\n  =<br \/>\n  +=<br \/>\n  -=<br \/>\n  *=<br \/>\n  \/=<br \/>\n  %=<\/p>\n<p>-Comparison<br \/>\n  ==<br \/>\n  === \/\/ of same type<br \/>\n  !=<br \/>\n  !== \/\/ of same type<br \/>\n  ><br \/>\n  <\n  >=<br \/>\n  <=\n\n-Logical\n  &#038;&#038;\n  ||\n  !\n\n<strong>Condition<\/strong><br \/>\n-if<br \/>\n  if (condition) {<br \/>\n    statements;<br \/>\n  }else{<br \/>\n    statements;<br \/>\n  }<\/p>\n<p>-switch<br \/>\n  switch (expression) {<br \/>\n  case label1:<br \/>\n    statements;<br \/>\n    break;<br \/>\n  case label2:<br \/>\n    statements;<br \/>\n    break;<br \/>\n  default:<br \/>\n    statements;<br \/>\n    break;<br \/>\n  }<\/p>\n<p><strong>Looping<\/strong><br \/>\n-while<br \/>\n  while (condition) {<br \/>\n    statements;<br \/>\n  }<\/p>\n<p>-do while<br \/>\n  do {<br \/>\n    statements;<br \/>\n  } while (condition);<\/p>\n<p>-for<br \/>\n  for (int i=0; i<5; i++) {\n    statements;\n  }\n\n<strong>exit and return<\/strong><br \/>\n-exit(&#8220;db fail&#8221;);<br \/>\n-die(&#8220;db fail&#8221;);<br \/>\n-$db = @mysql_connect(&#8220;localhost&#8221;, $USERNAME,$PASS)<br \/>\nor die(&#8220;db fail&#8221;);<\/p>\n<p><strong>Include code<\/strong><br \/>\n<? Include 'header.html'; ?><br \/>\n<? Require 'design.inc';\n  header(); ?><br \/>\ncontent<br \/>\n<? Footer(); ?><br \/>\n-allow_url_fopen in php.ini<br \/>\n-include_once\/require_once<br \/>\n-qet_included_files()<\/p>\n<p><strong>Embedding in web pages<\/strong><br \/>\n-Use echo<\/p>\n<pre lang=\"php\">\r\n<?php echo \"hello\"; ?>\r\n<\/pre>\n<p>-Use PHP Script<\/p>\n<pre lang=\"php\">\r\n<script lanquage=\"php\">\r\n  echo \"hello!\";\r\n<!script>\r\n<\/pre>\n<p>-echo directly<\/p>\n<pre lang=\"php\">\r\n<?= \"hello\"; ?>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Basic syntax Comments \/\/ for single line \/* *\/ for multiple line # for shell style comments Variables -starts with $ -may contain strings numbers arrays -is case sensitive Variable variables $foo = &#8216;bar&#8217;; $$foo = &#8216;baz&#8217;; Variable reference $black &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=95\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[20],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-1x","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=95"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}