{"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=IQoJb3JpZ2luX2VjEL7%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJGMEQCIFWt745nISKouSW3UNTUjRxUBEnwBoi99UrCi13DkTddAiBEOAU1puxUecCC%2BNVxvLqXb2fKPws%2F0WzZQheRgIhXlSrCBQiH%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAUaDDI3MzU3MDE5NTQzMyIMoLz%2FnL3I5PJeIizGKpYFnxMoQwqlzBWQNLCE0lQA%2FbtPxGmrt1kR8iO2M9jRSr9cSufA6qwRUL125cgLeaW3jAjWnLaJRnTbH6%2B3AugL6UlauZul0mKXPNjYWYuEbpbvKmSbw%2B52%2BbpyWgOjqUIe9Q2bUhgJ7t%2BnzE%2BlFI1p%2Fp9vm5gbE%2FE1yQph10OEhZ2Et8agtLlqAQXo%2FJbLO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%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260910T135034Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=c40371b676ee43b49ac0727d6f88d9dafd6fb0d9ed30aa2b8f07b5d97dca57de\" 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}]}}