{"id":1392,"date":"2018-01-30T18:40:58","date_gmt":"2018-01-30T23:40:58","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1392"},"modified":"2018-07-05T19:55:13","modified_gmt":"2018-07-06T00:55:13","slug":"python-php-comparison-language-basics","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/01\/30\/python-php-comparison-language-basics\/","title":{"rendered":"Python and PHP: A comparison of the language basics"},"content":{"rendered":"<p>In this table I will compare some basic programming syntax and conventions between the Python and PHP programming languages.<\/p>\n<table width=\"60%\" id=\"lang-compare-table\">\n<tbody>\n<tr>\n<th width=\"30%\">Programming element<\/th>\n<th width=\"35%\">Python<\/th>\n<th width=\"35%\">PHP<\/th>\n<\/tr>\n<tr>\n<td><strong>Code commenting<br \/>\n<\/strong>PHP supports Python and JavaScript conventions<\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Single line comment<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\"\"\"This is a multi-line comment block. This is a multi-line comment block.\"\"\"<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Single line comment<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\/\/ Single line comment<\/code><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\/*This is a multi-line comment block. This is a multi-line comment block.*\/<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a variable that contains an int:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">age = 25<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$age = 25;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a variable that contains a float (known as a &#8220;double&#8221; in PHP):<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">probability = 0.62<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$probability = 0.62;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a variable that contains a string:<\/strong><br \/>\nPython supports single, double, and triple quotes.<\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">state = 'North Dakota'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">state = \"North Dakota\"<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">state = \"\"\"North Dakota\"\"\"<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$state = 'North Dakota';<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$state = \"North Dakota\";<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Denoting string literals:<\/strong><br \/>\nPython uses an &#8216;r&#8217; in front of the string. PHP uses single quotes.<\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">path = r'\\path\\to\\some\\files'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">path = r\"\\path\\to\\some\\files\"<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">path = r\"\"\"\\path\\to\\some\\files\"\"\"<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$path = '\\path\\to\\some\\files'<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>String Concatenation:<\/strong><br \/>\nPHP uses a dot.<\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">greeting = \"Hello\" + \" world\"<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">greeting = str1 + str2<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$greeting = \"Hello\" . \" world\";<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$greeting = $str1 . $str2;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a list or array:<\/strong><br \/>\nPHP uses array() function.<\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_list = ['apple', 'banana', 'cherry']<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$fruit_list = array(\"apple\", \"banana\", \"cherry\");<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Getting the length of a list or array:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">length = len(fruit_list)<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$length = count($fruit_list); <\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$length = sizeof($fruit_list); <\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Python dictionary vs. PHP associative array<br \/>\n<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit = {'a':'apple', 'b':'banana', 'c':'cherry'}<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$fruit = array(\"a\" =&gt; \"apple\", \"b\" =&gt; \"banana\", \"c\" =&gt; \"cherry\");<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Getting keys from a dictionary or associative array:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_keys = fruit.keys()<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$fruit_keys = (array_keys($fruit));<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Getting values from a dictionary or associative array:<br \/>\n<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_values = fruit.values()<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$fruit_values = (array_values($fruit));<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Adding key-value pairs to a dictionary or associative array:<br \/>\n<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit['o'] = 'orange'<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$fruit['o'] = \"orange\";<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Deleting key-value pairs from a dictionary or associative array:<br \/>\n<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">del fruit['b']<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">unset($fruit['b']);<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Python was invented in the late 1980s (1989) by Guido van Rossum. Ideas for Python were adopted from Modula-3 and Lisp.<\/p>\n<p>Here is what the <a href=\"http:\/\/php.net\">PHP website<\/a> says about PHP:<\/p>\n<blockquote>\n<div class=\"blurb\">\n<p>PHP is a popular general-purpose scripting language that is especially suited to web development.<\/p>\n<p>Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.<\/p>\n<\/div>\n<\/blockquote>\n<p>Here is what wikipedia says about PHP:<\/p>\n<blockquote><p>PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive acronym PHP: Hypertext Preprocessor.<\/p><\/blockquote>\n<p>Here is what w3schools.com says about PHP:<\/p>\n<blockquote><p>PHP is a powerful server-side scripting language for creating dynamic and interactive websites. PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft&#8217;s ASP.<\/p>\n<p>PHP is perfectly suited for Web development and can be embedded directly into the HTML code.<\/p><\/blockquote>\n<p>For more information about some of the PHP built-in functions shown above, see:<br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/ref.array.php\">http:\/\/php.net\/manual\/en\/ref.array.php<\/a><br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.count.php\">http:\/\/php.net\/manual\/en\/function.count.php<\/a><br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.sizeof.php\">http:\/\/php.net\/manual\/en\/function.sizeof.php<\/a><br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.unset.php\">http:\/\/php.net\/manual\/en\/function.unset.php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this table I will compare some basic programming syntax and conventions between the Python and PHP programming languages. Programming element Python PHP Code commenting PHP supports Python and JavaScript conventions # Single line comment &#8220;&#8221;&#8221;This is a multi-line comment block. This is a multi-line comment block.&#8221;&#8221;&#8221; # Single line comment \/\/ Single line comment\/*This &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/01\/30\/python-php-comparison-language-basics\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python and PHP: A comparison of the language basics<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[69,4],"class_list":["post-1392","post","type-post","status-publish","format-standard","hentry","category-php-language","tag-php","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1392"}],"version-history":[{"count":35,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1392\/revisions"}],"predecessor-version":[{"id":2109,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1392\/revisions\/2109"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}