{"id":1233,"date":"2018-05-29T06:26:48","date_gmt":"2018-05-29T06:26:48","guid":{"rendered":"http:\/\/frowningbear.com\/codebase\/?p=1233"},"modified":"2018-05-29T06:52:08","modified_gmt":"2018-05-29T06:52:08","slug":"php-notes","status":"publish","type":"post","link":"https:\/\/frowningbear.com\/codebase\/2018\/05\/29\/php-notes\/","title":{"rendered":"PHP Notes"},"content":{"rendered":"<p>All keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive, but all variable names are case-sensitive.<\/p>\n<p>A variable starts with the $ sign, followed by the name of the variable<br \/>\nA variable name must start with a letter or the underscore character<br \/>\nA variable name cannot start with a number<br \/>\nA variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )<br \/>\nVariable names are case-sensitive ($age and $AGE are two different variables)<\/p>\n<p><!--more--><\/p>\n<p>PHP has three different <strong>variable scopes<\/strong>:<\/p>\n<p><strong>local<\/strong><br \/>\n<strong> global<\/strong><br \/>\n<strong> static<\/strong><\/p>\n<p>A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.<\/p>\n<p>A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.<\/p>\n<p>To access a global variable from within a function, use the global before the variables:<\/p>\n<pre>$x = 5;\r\n$y = 10;\r\n\r\nfunction myTest() {\r\n    global $x, $y;\r\n    $y = $x + $y;\r\n}\r\n\r\nmyTest();\r\necho $y; \/\/ 15\r\n<\/pre>\n<p>PHP also stores all global variables in an array called <strong>$GLOBALS[index]<\/strong>. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.<\/p>\n<p>The example above can be rewritten like this:<\/p>\n<pre>$x = 5;\r\n$y = 10;\r\n\r\nfunction myTest() {\r\n$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];\r\n}\r\n\r\nmyTest();\r\necho $y; \/\/ 15\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>The static Keyword<\/h3>\n<p>Normally, when a function is completed\/executed, all of its variables are deleted. If you want the variable to persist, however, you can use the static keyword when you first declare the variable:<\/p>\n<pre>function myTest() {\r\nstatic $x = 0;\r\necho $x;\r\n$x++;\r\n}\r\n\r\nmyTest();\r\nmyTest();\r\nmyTest();\r\n\r\n<\/pre>\n<p>Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.<\/p>\n<p>Note: The variable is still local to the function.<\/p>\n<p>&nbsp;<\/p>\n<h3>echo and print Statements<\/h3>\n<p>echo and print are more or less the same. They are both used to output data to the screen.<\/p>\n<p>The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.<\/p>\n<p>The echo statement can be used with or without parentheses: echo or echo().<br \/>\nThe print statement can be used with or without parentheses: print or print().<\/p>\n<p>&nbsp;<\/p>\n<h3>Data Types<\/h3>\n<p>Variables can store data of different types, and different data types can do different things.<\/p>\n<p>PHP supports the following data types:<\/p>\n<p>String<br \/>\nInteger<br \/>\nFloat (floating point numbers &#8211; also called double)<br \/>\nBoolean<br \/>\nArray<br \/>\nObject<br \/>\nNULL<br \/>\nResource<\/p>\n<p><strong>String<\/strong><\/p>\n<p>A string is a sequence of characters, like &#8220;Hello world!&#8221;.  A string can be any text inside quotes, either double or single.<\/p>\n<p><strong>Integer<\/strong><\/p>\n<p>An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.<\/p>\n<p>An integer must have at least one digit<br \/>\nAn integer must not have a decimal point<br \/>\nAn integer can be either positive or negative<br \/>\nIntegers can be specified in three formats: decimal (10-based), hexadecimal (16-based &#8211; prefixed with 0x) or octal (8-based &#8211; prefixed with 0)<\/p>\n<p>In the following example $x is an integer. The PHP var_dump() function returns the data type and value:<\/p>\n<p>$x = 5985;<\/p>\n<p><strong>Float<\/strong><\/p>\n<p>A float (floating point number) is a number with a decimal point or a number in exponential form.<\/p>\n<p>In the following example $x is a float. The  var_dump() function returns the data type and value:<\/p>\n<p>$x = 10.365;<br \/>\nvar_dump($x);<\/p>\n<p><strong>Boolean<\/strong><\/p>\n<p>A Boolean represents two possible states: TRUE or FALSE.<br \/>\n$x = true;<br \/>\n$y = false;<\/p>\n<p><strong>Array<\/strong><\/p>\n<p>An array stores multiple values in one single variable.<\/p>\n<p><strong>Object<\/strong><\/p>\n<p>An object is a data type which stores data and information on how to process that data.<\/p>\n<p>Objects must be explicitly declared.<\/p>\n<p>First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods:<\/p>\n<pre>class Car {\r\nfunction Car() {\r\n$this-&gt;model = \"VW\";\r\n}\r\n}\r\n\r\n\/\/ create an object\r\n$herbie = new Car();\r\n\r\n\/\/ show object properties\r\necho $herbie-&gt;model;\r\n\r\n<\/pre>\n<p><strong>NULL<\/strong><\/p>\n<p>Null is a special data type which can have only one value: NULL.<\/p>\n<p>A variable of data type NULL is a variable that has no value assigned to it.<\/p>\n<p>Tip: If a variable is created without a value, it is automatically assigned a value of NULL.<\/p>\n<p>Variables can also be emptied by setting the value to NULL:<\/p>\n<pre>$x = \"Hello world!\";\r\n$x = null;\r\nvar_dump($x);\r\n\r\n<\/pre>\n<p><strong>Resource<\/strong><\/p>\n<p>The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.<\/p>\n<p>A common example of using the resource data type is a database call.<\/p>\n<p>&nbsp;<\/p>\n<h3>Constants<\/h3>\n<p>Constants are like variables except that once they are defined they cannot be changed or undefined.<\/p>\n<p>A constant is an identifier (name) for a simple value. The value cannot be changed during the script.<\/p>\n<p>A valid constant name starts with a letter or underscore (<strong>no $ sign before the constant name<\/strong>).<\/p>\n<p>Note: Unlike variables, constants are automatically global across the entire script.<\/p>\n<p>Default for case-insensitive is false.<\/p>\n<pre>define(\"GREETING\", \"Hello!\", true);\r\necho greeting;\r\n\r\nfunction myTest() {\r\necho GREETING;\r\n}\r\n\r\nmyTest();\r\n\r\n<\/pre>\n<h3>Sort Functions For Arrays<\/h3>\n<p>sort() &#8211; sort arrays in ascending order<br \/>\nrsort() &#8211; sort arrays in descending order<br \/>\nasort() &#8211; sort associative arrays in ascending order, according to the value<br \/>\nksort() &#8211; sort associative arrays in ascending order, according to the key<br \/>\narsort() &#8211; sort associative arrays in descending order, according to the value<br \/>\nkrsort() &#8211; sort associative arrays in descending order, according to the key<\/p>\n<p>&nbsp;<\/p>\n<h3>Superglobals<\/h3>\n<p>Several predefined variables in PHP are &#8220;superglobals&#8221;, which means that they are always accessible, regardless of scope &#8211; and you can access them from any function, class or file.<\/p>\n<p>The PHP superglobal variables are:<\/p>\n<p>$GLOBALS<br \/>\n$_SERVER<br \/>\n$_REQUEST<br \/>\n$_POST<br \/>\n$_GET<br \/>\n$_FILES<br \/>\n$_ENV<br \/>\n$_COOKIE<br \/>\n$_SESSION<\/p>\n<p>&nbsp;<\/p>\n<h3>GET vs. POST<\/h3>\n<p>Both create an associative array holding key\/value pairs, where keys are the names of the form controls and values are the input data from the user.<\/p>\n<p><strong>$_GET<\/strong> and <strong>$_POST<\/strong>  are both superglobals, which means that they are always accessible, regardless of scope.<\/p>\n<p>When to use GET?<\/p>\n<p>Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.<\/p>\n<p>GET should only be used for sending non-sensitive data, definitely not passwords etc.<\/p>\n<p>When to use POST?<\/p>\n<p>Information sent from a form with the POST method is invisible to others (all names\/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.<\/p>\n<p>Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.<\/p>\n<p>However, because the variables are not displayed in the URL, it is not possible to bookmark the page.<\/p>\n<h3>SALT<\/h3>\n<p>Password salting is a technique which is widely used to secure passwords by randomizing password hashes, so that if two users have the same password, they will not have the same password hashes. This is done by appending or prepending a random string, called a salt, to the password before hashing.<\/p>\n<pre class=\"wrap:true lang:default decode:true \" title=\"PHP Reference\">&lt;?php\r\ndate_default_timezone_set( \"Australia\/Sydney\" ); \r\n\r\n\/\/if...else...elseif\r\n$time = date(\"H\");\r\n\r\nif ($time &lt; \"10\") {\r\n    echo \"Have a good morning!\";\r\n} elseif ($time &lt; \"20\") {\r\n    echo \"Have a good day!\";\r\n} else {\r\n    echo \"Have a good night!\";\r\n}\r\n\r\n\/\/Use the switch statement to select one of many blocks of code to be executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.\r\n$favcolor = \"red\";\r\n\r\nswitch ($favcolor) {\r\n    case \"red\":\r\n        echo \"Your favorite color is red!\";\r\n        break;\r\n    case \"blue\":\r\n        echo \"Your favorite color is blue!\";\r\n        break;\r\n    case \"green\":\r\n        echo \"Your favorite color is green!\";\r\n        break;\r\n    default:\r\n        echo \"Your favorite color is neither red, blue, nor green!\";\r\n}\r\n\r\n\r\n\/\/while loop\r\n$x = 1;\r\n\r\nwhile($x &lt;= 5) {\r\n    echo \"The number is: $x &lt;br&gt;\";\r\n    $x++;\r\n} \r\n\r\n\/\/do...while loop\r\n\r\n\/\/The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.\r\n\r\n$x = 1;\r\n\r\ndo {\r\n    echo \"The number is: $x &lt;br&gt;\";\r\n    $x++;\r\n} while ($x &lt;= 5);\r\n\r\n\r\n\/\/for loop\r\n\r\n\/\/The for loop is used when you know in advance how many times the script should run.\r\n\r\nfor ($x = 0; $x &lt;= 10; $x++) {\r\n    echo \"The number is: $x &lt;br&gt;\";\r\n} \r\n\r\n\/\/foreach loop\r\n\r\n\/\/The foreach loop works only on arrays, and is used to loop through each key\/value pair in an array.\r\n\r\n$colors = array(\"red\", \"green\", \"blue\", \"yellow\");\r\n\r\nforeach ($colors as $value) {\r\n    echo \"$value &lt;br&gt;\";\r\n}\r\n\r\n\r\n\/\/Default arguments in functions\r\n\r\nfunction setHeight($minheight = 50) {\r\n    echo \"The height is : $minheight &lt;br&gt;\";\r\n}\r\n\r\nsetHeight(350); \/\/ 350\r\nsetHeight(); \/\/ will use the default value of 50\r\n\r\n\r\n\/*\r\nSuperglobal variables:\r\n\r\n$GLOBALS\r\n$_SERVER\r\n$_REQUEST\r\n$_POST\r\n$_GET\r\n$_FILES\r\n$_ENV\r\n$_COOKIE\r\n$_SESSION\r\n*\/\r\n\r\n\r\n\/*\r\nThree types of arrays:\r\n\r\nIndexed arrays - Arrays with a numeric index\r\nAssociative arrays - Arrays with named keys\r\nMultidimensional arrays - Arrays containing one or more arrays\r\n*\/\r\n\r\n\/\/array() function creates an array\t\r\n$arr = array();\t\r\n\t\r\n$myarray = array(); \/\/this line necessary?\r\n$myarray[0] = \"Vanessa\";\r\n$myarray[1] = \"Alice\";\r\n$myarray[2] = \"Rebecca\";\r\n\r\n\/\/Or\r\n\r\n#$myarray = (\"Vanessa\", \"Alice\", \"Rebecca\");\r\n\r\nprint_r($myarray);\r\n\r\nvar_dump($myarray);\r\n\r\n\/\/get the length of an array\r\necho count($myarray) . \"&lt;br&gt;\";\r\n\r\n\/\/add item\r\n$myarray[] = \"Kelly\";\r\n\r\narray_push($myarray, \"Chloe\");\r\nvar_dump($myarray);\r\n\r\n\/\/implode()\r\necho implode(\" - \", $myarray) . \"&lt;br&gt;\";\r\n\r\n\r\n\r\n\/\/loop indexed array\r\n$arrlength = count($myarray);\r\nfor($i = 0; $i &lt; $arrlength; $i++) {\r\n\techo $myarray[$i] . \"&lt;br&gt;\";\r\n}\r\n\r\n\/\/foreach\r\nforeach($myarray as $name) {\r\n\techo $name . \"&lt;br&gt;\";\r\n\techo \"Name is: $name &lt;br&gt;\";\r\n\techo \"NAME is {$name} &lt;br&gt;\";\r\n}\r\n\r\n\/\/while loop\r\n$num = 3;\r\n$i = 0;\r\nwhile ($i &lt; $num) {\r\n\t$namenum = $i + 1;\r\n\techo \"Name $namenum is: $myarray[$i]&lt;br&gt;\";\r\n\t++$i;\r\n}\r\n\r\n\r\necho \"&lt;h3&gt;Associative&lt;\/h3&gt;\";\r\n\r\n\r\n\/\/create an associate array\r\n$age = array(\r\n    \"Vanessa\" =&gt; 35, \r\n    \"Alice\" =&gt; 25, \r\n    \"Rebecca\" =&gt; \"twenty eight\"\r\n);\r\n\r\n\/\/Or\r\n$age = array();\r\n$age['Vanessa'] = 35;\r\n$age['Alice'] = 25;\r\n$age['Rebecca'] = \"twenty eight\";\r\n\r\n\/\/loop associative array\r\nforeach($age as $key =&gt; $value) {\r\n\techo $key . \" : \" . $value . \"&lt;br&gt;\";\r\n}\r\n\r\necho \"&lt;h3&gt;Multi-dimensional&lt;\/h3&gt;\";\r\n\r\n$flower_shop = array(\r\n\"rose\" =&gt; array( \"5.00\", \"7 items\", \"red\" ),\r\n\"daisy\" =&gt; array( \"4.00\", \"3 items\", \"blue\" ),\r\n\"orchid\" =&gt; array( \"2.00\", \"1 item\", \"white\" ),\r\n);\r\n\r\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>All keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive, but all variable names are case-sensitive. A variable starts with the $ sign, followed by the name of the variable A variable name must start with a letter or the underscore character A variable name cannot start with a &hellip; <a href=\"https:\/\/frowningbear.com\/codebase\/2018\/05\/29\/php-notes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP Notes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1233","post","type-post","status-publish","format-standard","hentry","category-php"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/1233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/comments?post=1233"}],"version-history":[{"count":2,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/1233\/revisions"}],"predecessor-version":[{"id":1241,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/1233\/revisions\/1241"}],"wp:attachment":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/media?parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/categories?post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/tags?post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}