{"id":96,"date":"2008-03-15T23:14:25","date_gmt":"2008-03-15T17:44:25","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/2008\/03\/hello-script-for-php\/"},"modified":"2008-03-15T23:14:25","modified_gmt":"2008-03-15T17:44:25","slug":"hello-script-for-php","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2008\/03\/hello-script-for-php\/","title":{"rendered":"Hello Script for PHP"},"content":{"rendered":"<p class=\"intro\">In the last post I introduced the concept of <strong>&#8216;Hello Script&#8217; &#8211; a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet<\/strong> when working with that language. I thought I can elaborate on that concept by creating Hello Scripts for all the languages that I am familiar with.<\/p>\n<p>Let&#8217;s start with PHP &#8211; I already provided this as an example for the last post. Here is the entire PHP Hello Script&#8230;<\/p>\n<pre><code class=\"php\">\n&lt;?php\n\/\/ Printing(IO)\nprint \"Hello World!\\n\";\n\n\/\/ Variables, concatenation\n$name = 'Binny';\n$year = 2008;\nprint \"Hello, \" . $name . \" - welcome to \" . $year . \"\\n\";\n\n\/\/If,else conditions\nif ($year &gt; 2008) {\n\tprint \"Welcome to the future - yes, we have flying cars!\";\n}\nelse if($year &lt; 2008) {\n\tprint \"The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!\";\n}\nelse {\n\tprint \"Anything wrong with your time machine? You have not gone anywhere, kiddo.\";\n}\nprint \"\\n\";\n\n\/\/ For loop\nfor($i=0; $i&lt;3; $i++) {\n\tprint \"$i) Hi there!\\n\";\n}\n\n\/\/Numerical Array, While\n$rules = array(\n\t'Do no harm',\n\t'Obey',\n\t'Continue Living'\n);\n$i = 0;\nwhile($i&lt;count($rules)) {\n\tprint \"Rule \" . ($i+1) . \" : \" . $rules[$i] . \"\\n\";\n\t$i++;\n}\n\n\/\/ Associated array, foreach\n$associated = array(\n\t'hello'\t=&gt;\t'world',\n\t'foo'\t=&gt;\t'bar',\n\t'lorem'\t=&gt;\t'ipsum'\n);\nforeach($associated as $key =&gt; $value) {\n\tprint \"$key: $value\\n\";\n}\n\n\/\/ Using Join and Split\n$csv_values = explode(',', \"hello,world,how,are,you\\n\");\nprint implode(\":\", $csv_values);\n\n\n\/\/ Function, argument, return, call\nfunction hello($name) {\n\treturn \"Hello \" . $name;\n}\n$hello_string = hello(\"Binny\");\n\n\/\/ One for the OOP fanboys - Class, members, object and stuff.\nclass Movie {\n\tpublic $name = '';\n\tpublic $rating = 0;\n\t\n\tfunction __construct($name) {\n\t\t$this-&gt;name = $name;\n\t\t$this-&gt;rateMovie();\n\t}\n\tfunction rateMovie() {\n\t\t$this-&gt;rating = (strlen($this-&gt;name) % 10) + 1; \/\/IMDBs rating algorithm. True story!\n\t}\n\t\n\tfunction printMovieDetails() {\n\t\tprint \"Movie : {$this-&gt;name}\\n\";\n\t\tprint \"Rating : \" . str_repeat('*', $this-&gt;rating) . \"({$this-&gt;rating})\\n\\n\";\n\t}\n}\n\/\/Create the object\n$ncfom = new Movie(\"New Country for Old Men\"); \/\/It's a sequel!\n$ncfom-&gt;printMovieDetails();\n\n\/\/ File IO\n\/\/ File reading, easy method...\n$contents = file_get_contents('Hello.php');\nprint \"Hello has \" . strlen($contents) . \" chars\\n\";\n\/\/ Writing to a file\n$file_handle = f<span><\/span>open('\/tmp\/hello.txt', 'w');\nfputs($file_handle, \"Hello World\");\nfclose($file_handle);\n\n\n\/\/ Command Executing\nprint `ls`; \/\/Execute the command 'ls' and print its output\nprint \"\\n\";\n\n\/\/ Regular Expressions\n$string = \"Hello World\";\nif(preg_match('\/^Hell\/', $string)) print \"Yup - its evil\\n\";\nprint preg_replace('\/l([^l])\/', \"$1\", $string); \/\/Remove an 'l' from both words. Should print 'Helo Word'\n\n\n\/*\n * Specialized code\n *\/\n\/\/ Database connectivity(native)\nmysql_connect('localhost', 'root', '') or die(\"Cannot connect to the Database server\");\nmysql_select_db('Data') or die('Could not find a database called \"Data\"');\n\n\/\/ Executing Query\n$sql_handle = mysql_query('SELECT name,url,description FROM Comment LIMIT 1') or die('Query Error: ' . mysql_error());\nwhile($row = mysql_fetch_assoc($sql_handle)) {\n\tprint \"Name\t:\t$row[name]\\n\";\n\tprint \"URL\t:\t$row[url]\\n\";\n\tprint \"Desc\t:\t$row[description]\\n\\n\";\n}\n\/* Just a note here - if you are going to use PHP with database in a production \n * system(and you will, trust me), use a Database abstraction layer rather than \n * the above mentioned native methods.\n *\/\n\nprint $_REQUEST['username']; \/\/ Method to get the value of the field 'username' after a form submit. Will not work at CLI execution\n<\/code><\/pre>\n<p>Save this to a file and keep it around for future reference &#8211; if you are just starting out with PHP<\/p>\n<p>Coming up next &#8211; Python Hello Script.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In the last post I introduced the concept of &#8216;Hello Script&#8217; &#8211; a file that contains the most commonly used elements of a programming language <a class=\"mh-excerpt-more\" href=\"https:\/\/www.bin-co.com\/blog\/2008\/03\/hello-script-for-php\/\" title=\"Hello Script for PHP\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,21,25,30],"tags":[137,210,245,289],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-mysql","category-php","category-scripts","category-web-development","tag-hello","tag-php","tag-script","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}