{"id":330,"date":"2021-06-09T19:03:49","date_gmt":"2021-06-09T18:03:49","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/?page_id=330"},"modified":"2021-06-10T13:32:11","modified_gmt":"2021-06-10T12:32:11","slug":"python-to-php","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/php\/python-to-php\/","title":{"rendered":"Python to PHP"},"content":{"rendered":"<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-196\" src=\"https:\/\/blogs.glowscotland.org.uk\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/05211152\/sqa-ah.png\" alt=\"\" width=\"100\" height=\"97\" \/><a href=\"https:\/\/www.w3schools.com\/pHp\/default.asp\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"image-59 alignright\" src=\"https:\/\/glow-prod-sh.s3.eu-west-1.amazonaws.com\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/03210120\/w3schools-150x150.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&#038;X-Amz-Security-Token=IQoJb3JpZ2luX2VjEE4aCWV1LXdlc3QtMSJIMEYCIQDAjyc2GFW9mtKLH5lvn2lJoFZkG%2FQG8We6TeatFFTIBwIhAPSa7uFCP%2BwuRcJxr1mnmbZ96T8R60zkrtqIo9MBGM4HKrkFCBcQBRoMMjczNTcwMTk1NDMzIgywzb%2F%2FSx6r7lo6WksqlgW4PoeDWtQM%2FQfHaQ9Bx1%2FKRn5l0hfKMo5A%2FxBdpIL8nhd%2BZ2Z8V8SLHg7fu3A%2F00rCUJcFKGMFnBlxwKbI%2Fo0F2yeVeS6l0CG5IDTa2AjBtuHAQQnBkEcstbEJ1fFwrt7pb39ctqWH7OIQm1XYEBAUpQWAXD1rZAHKPazYIsde0ka9raeU78BJafzxi%2BXoYdfeNPn%2B8LPX88hlkeljD5dQEaM%2F6qC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fs3%2Faws4_request&#038;X-Amz-Date=20260420T064418Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=8e856693c924a8dfc6569b19116f4bf78fca8b479b196480d689b7b03b530a0f\" alt=\"\" width=\"50\" height=\"47\" \/><\/a><\/h1>\n<p>There are many similarities between Python and PHP, but there significant differences:<\/p>\n<ul>\n<li>PHP variables start with $ signs<\/li>\n<li>PHP statements must end with a semi-colon<\/li>\n<li>Instead of indentation, PHP uses pairs of curly brackets { } to enclose blocks of code inside conditional statements and loops.<\/li>\n<li>Conditions are enclosed in parentheses ( ).<\/li>\n<\/ul>\n<h1>Case Sensitivity<\/h1>\n<ul>\n<li>PHP keywords are not case sensitive (ECHO = echo = Echo)<\/li>\n<li>PHP variables are case sensitive ($counter != $Counter)<\/li>\n<\/ul>\n<h1>Comments<\/h1>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\n# A single line comment \r\n''' A multi \r\nline comment ''' <\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n\/\/ A single line comment\r\n# Another single line comment \r\n\/* A multi\r\nline comment *\/ \r\n<\/pre>\n<h1>Printing<\/h1>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\nprint(f&quot;Counter = {counter}&quot;)\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\necho &quot;Counter = &quot;.$counter;   \/* using concatenation *\/\r\necho &quot;Counter = $counter&quot;;    \/* simple variables inside string *\/\r\n<\/pre>\n<h1>Assignment<\/h1>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\ncounter = 0\r\npassword = &quot;xyz&quot;\r\npassword = 'xyz'\r\n\r\nMAXSTUDENTS = 20\r\n\r\ncolours = &#x5B;&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;]\r\n \r\ncomputerIDs = &#x5B;&quot;&quot;] * 10\r\ncomputerId&#x5B;0] = &quot;AHS-210012&quot;\r\n \r\nfruits = &#x5B;]\r\nfruits.append(&quot;Apple&quot;)\r\n\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$counter = 0;\r\n$password = &quot;xyz&quot;;\r\n\r\ndefine(&quot;MAXSTUDENTS&quot;, 20);\r\n\r\n$colours = array(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;);\r\n \r\n$computerIDs = array_fill(0, 10, &quot;&quot;);\r\n$computerId&#x5B;0] = &quot;AHS-210012&quot;;\r\n \r\n$fruits = array();\r\narray_push($fruits, &quot;Apple&quot;);\r\n\r\n<\/pre>\n<h1>Operators<\/h1>\n<p>Arithmetic operators + &#8211; * \/\u00a0 ** (exponentiation) % (modulus) are same in Python and PHP<\/p>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\ntotal = total + number\r\ntotal += number\r\n \r\nfullname = forename + &quot; &quot; + surname\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$total = $total + $number;\r\n$total += $number;\r\n \r\n$fullname = $forename . &quot; &quot; . $surname;\r\n<\/pre>\n<h1>Predefined\u00a0 Functions<\/h1>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\nnameLength = len(fullname)\r\ndice = random.randint(1,6)\r\nnoOfFruits = len(fruits)\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$nameLength = strlen($fullname);\r\n$dice = rand(1,6);\r\n$noOfFruits = count($fruits);\r\n<\/pre>\n<h1>Conditional Statements<\/h1>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\nif guess == number:\r\n     print(&quot;You guessed the number&quot;)\r\nelif guess &lt; number:\r\n    print(&quot;Your guess is too small&quot;)\r\nelse\r\n    print(&quot;Your guess is too big&quot;)\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\nif ($guess == $number) {\r\n    echo &quot;You guessed the number&quot;;\r\n} elseif ($guess &lt; $number) {\r\n    echo &quot;Your guess is too small&quot;;\r\n} else {\r\n    echo &quot;Your guess is too big&quot;;\r\n}\r\n<\/pre>\n<h1>Switch Statements<\/h1>\n<p>Python does not have a switch statement. The simplest equivalent is an if..elif..elif..else statement.<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\nswitch ($favcolor) {\r\n  case &quot;red&quot;:\r\n    echo &quot;Your favourite colour is red!&quot;;\r\n    break;\r\n  case &quot;blue&quot;:\r\n    echo &quot;Your favourite colour is blue!&quot;;\r\n    break;\r\n  case &quot;green&quot;:\r\n    echo &quot;Your favourite colour is green!&quot;;\r\n    break;\r\n  default:\r\n    echo &quot;Your favourite colour is neither red, blue, nor green!&quot;;\r\n}\r\n<\/pre>\n<h1>Fixed Loops (by index)<\/h1>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\nfor x in range(10):\r\n    print(x)\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\nfor ($x = 0; $x &lt; 10; $x++) {\r\n  echo &quot;&lt;p&gt;$x&lt;\/p&gt;\\n&quot;;\r\n}\r\n<\/pre>\n<h1>Fixed Loops (by item)<\/h1>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\ncolours = &#x5B;&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;]\r\nfor colour in colours:\r\n    print(colour)\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\ncolours = array(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;);\r\nforeach ($colours as $colour) {\r\n  echo &quot;&lt;p&gt;$colour&lt;\/p&gt;\\n&quot;;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are many similarities between Python and PHP, but there significant differences: PHP variables start with $ signs PHP statements must end with a semi-colon Instead of indentation, PHP uses pairs of curly brackets { } to enclose blocks of code inside conditional statements and loops. Conditions are enclosed in parentheses ( ). Case Sensitivity PHP keywords are not case sensitive (ECHO = echo = Echo) PHP variables are case<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/php\/python-to-php\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":343,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-330","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":21,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/330\/revisions"}],"predecessor-version":[{"id":385,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/330\/revisions\/385"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/343"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/media?parent=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}