{"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 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 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=IQoJb3JpZ2luX2VjEK%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJIMEYCIQC0Rle7lLfae7oHUSTXlTYzV%2BDbdi8e6rx0rgK35JsPKQIhAK%2BfxguiN22mc8WEud60IJnk97LDt1wdo7Ahrdv0kGksKrgFCHgQBRoMMjczNTcwMTk1NDMzIgxAoQyfLe8yhbYpPSUqlQX6F1hlLi0CvIsAX8Rxln3JmxRqp%2Bpr2Q%2FW81qv5nevok9%2BlUSwIJzgyu%2FrgH6%2BV1ZyFj8VLrvS909ykJ%2Ftvyzxv%2FbICKUeQmMd1RyuE8hRVnohk%2B1Mqeu6%2BzjKThVvqJ%2BQA%2F1eQ52CIDw1OJM1KaWSGa6umGpK1AWuL1ARXAxcmwpOOpuc40bxs5hV9eK0p31e%2FL%2F2rNENA%2B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038;X-Amz-Date=20261022T153837Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=f1801717805446cc171df84d8731af7744f68ae69d1392feb3b7f15480f1fe1d\" 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}]}}