{"id":1659,"date":"2009-08-16T04:09:23","date_gmt":"2009-08-16T02:09:23","guid":{"rendered":"http:\/\/localhost\/helpcentre\/?p=1659"},"modified":"2023-04-20T12:10:02","modified_gmt":"2023-04-20T10:10:02","slug":"how-do-variables-work-in-php","status":"publish","type":"post","link":"https:\/\/xneelo.co.za\/help-centre\/website\/how-do-variables-work-in-php\/","title":{"rendered":"How do variables work in PHP?"},"content":{"rendered":"<p><b>Variables<\/b> are assigned in a typical manner. Normal variables are preceded with a $ and assigned to with =. Any variable can be used in an array by including a key in square brackets after the variable.<\/p>\n<p>For a regular array, a number is used as the key.<br \/>\nFor an associative array, a quotes enclosed string (or another variable) is used.<br \/>\nThe function \u2018array\u2019 can also be used to create arrays of either type.<\/p>\n<p>The code below illustrates these features (lines beginning with \u201c\/\/\u201d are comments):<\/p>\n<div class=\"codeblock\"><code><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #ff8000;\">\/\/ Normal variable assignment<br \/>\n<\/span><span style=\"color: #0000bb;\">$person <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #dd0000;\">\"Bob\"<\/span><span style=\"color: #007700;\">;<\/span><\/span><\/code><\/code><code><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ Assigning to a regular array<br \/>\n<\/span><span style=\"color: #0000bb;\">$fruit[0] <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #dd0000;\">\"apple\"<\/span><span style=\"color: #007700;\">;<br \/>\n<\/span><span style=\"color: #0000bb;\">$fruit[1] <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #dd0000;\">\"grape\"<\/span><span style=\"color: #007700;\">;<br \/>\n<\/span><span style=\"color: #0000bb;\">$fruit[2] <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #dd0000;\">\"orange\"<\/span><span style=\"color: #007700;\">;<\/span><\/span><\/code><\/code><code><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ Assigning to an associative array<br \/>\n<\/span><span style=\"color: #0000bb;\">$mother[<\/span><span style=\"color: #dd0000;\">'kitten'<\/span><span style=\"color: #0000bb;\">] <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #dd0000;\">\"cat\"<\/span><span style=\"color: #007700;\">;<br \/>\n<\/span><span style=\"color: #0000bb;\">$mother[<\/span><span style=\"color: #dd0000;\">'puppy'<\/span><span style=\"color: #0000bb;\">] <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #dd0000;\">\"dog\"<\/span><span style=\"color: #007700;\">;<\/span><\/span><\/code><\/code><\/p>\n<p><code><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ Using the array function for a regular array<br \/>\n<\/span><span style=\"color: #0000bb;\">$fruit <\/span><span style=\"color: #007700;\">= array(<\/span><span style=\"color: #dd0000;\">'apple'<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">'grape'<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">'orange'<\/span><span style=\"color: #007700;\">);<\/span><\/span><\/code><\/code><\/p>\n<p><code><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ Using the array function for an associative array<br \/>\n<\/span><span style=\"color: #0000bb;\">$mother <\/span><span style=\"color: #007700;\">= array(<\/span><span style=\"color: #dd0000;\">'kitten' <\/span><span style=\"color: #007700;\">=&gt; <\/span><span style=\"color: #dd0000;\">'cat'<\/span><span style=\"color: #007700;\">, <\/span><span style=\"color: #dd0000;\">'puppy' <\/span><span style=\"color: #007700;\">=&gt; <\/span><span style=\"color: #dd0000;\">'dog'<\/span><span style=\"color: #007700;\">);<\/span><\/span><\/code><\/code><\/p>\n<p><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ The following line prints \"Bob's cat ate an orange.\"<br \/>\n<\/span><span style=\"color: #007700;\">echo <\/span><span style=\"color: #dd0000;\">\"<\/span><span style=\"color: #0000bb;\">$person<\/span><span style=\"color: #dd0000;\">'s <\/span><span style=\"color: #0000bb;\">$mother[<\/span><span style=\"color: #dd0000;\">'kitten'] ate an <\/span><span style=\"color: #0000bb;\">$fruit[2]<\/span><span style=\"color: #dd0000;\">.\"<\/span><span style=\"color: #007700;\">;<br \/>\n<\/span><span style=\"color: #0000bb;\">?&gt; <\/span><br \/>\n<\/span><br \/>\n<\/code><\/p>\n<\/div>\n<p>One of the most powerful features of PHP is its easy access to requested variables. For instance, consider the following short form:<\/p>\n<div class=\"codeblock\"><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #007700;\">&lt;<\/span><span style=\"color: #0000bb;\">form action<\/span><span style=\"color: #007700;\">=<\/span><span style=\"color: #dd0000;\">\"form_handler.php?posted=true\" <\/span><span style=\"color: #0000bb;\">method<\/span><span style=\"color: #007700;\">=<\/span><span style=\"color: #dd0000;\">\"post\"<\/span><span style=\"color: #007700;\">&gt;<br \/>\n&lt;<\/span><span style=\"color: #0000bb;\">input name<\/span><span style=\"color: #007700;\">=<\/span><span style=\"color: #dd0000;\">\"formvar\"<\/span><span style=\"color: #007700;\">&gt;<br \/>\n&lt;<\/span><span style=\"color: #0000bb;\">input value<\/span><span style=\"color: #007700;\">=<\/span><span style=\"color: #dd0000;\">\"Submit Query\" <\/span><span style=\"color: #0000bb;\">type<\/span><span style=\"color: #007700;\">=<\/span><span style=\"color: #dd0000;\">\"submit\"<\/span><span style=\"color: #007700;\">&gt;<br \/>\n&lt;\/<\/span><span style=\"color: #0000bb;\">form<\/span><span style=\"color: #007700;\">&gt; <\/span><br \/>\n<\/span><br \/>\n<\/code><\/div>\n<p>On submitting the form, the value of the variable \u201cformvar\u201d will be available to the form_handler.php script simply as<\/p>\n<div class=\"codeblock\"><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">$_POST[<\/span><span style=\"color: #dd0000;\">'formvar'<\/span><span style=\"color: #0000bb;\">] <\/span><br \/>\n<\/span><br \/>\n<\/code><\/div>\n<p>and the variable posted simply<\/p>\n<div class=\"codeblock\"><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">$_GET[<\/span><span style=\"color: #dd0000;\">'posted'<\/span><span style=\"color: #0000bb;\">]<\/span><span style=\"color: #007700;\">. <\/span><br \/>\n<\/span><br \/>\n<\/code><\/div>\n<p>This is true for all POST and GET form calls.<\/p>\n","protected":false,"plain":"<b>Variables<\/b> are assigned in a typical manner. Normal variables are preceded with a $ and assigned to with =. Any variable can be used in an array by including a key in square brackets after the variable.\r\n\r\nFor a regular array, a number is used as the key.\r\nFor an associative array, a quotes enclosed string (or another variable) is used.\r\nThe function \u2018array\u2019 can also be used to create arrays of either type.\r\n\r\nThe code below illustrates these features (lines beginning with \u201c\/\/\u201d are comments):\r\n<div class=\"codeblock\"><code><code><span >\r\n<span >\/\/ Normal variable assignment\r\n<\/span><span >$person <\/span><span >= <\/span><span >\"Bob\"<\/span><span >;<\/span><\/span><\/code><\/code><code><code><span ><span >\/\/ Assigning to a regular array\r\n<\/span><span >$fruit[0] <\/span><span >= <\/span><span >\"apple\"<\/span><span >;\r\n<\/span><span >$fruit[1] <\/span><span >= <\/span><span >\"grape\"<\/span><span >;\r\n<\/span><span >$fruit[2] <\/span><span >= <\/span><span >\"orange\"<\/span><span >;<\/span><\/span><\/code><\/code><code><code><span ><span >\/\/ Assigning to an associative array\r\n<\/span><span >$mother[<\/span><span >'kitten'<\/span><span >] <\/span><span >= <\/span><span >\"cat\"<\/span><span >;\r\n<\/span><span >$mother[<\/span><span >'puppy'<\/span><span >] <\/span><span >= <\/span><span >\"dog\"<\/span><span >;<\/span><\/span><\/code><\/code>\r\n\r\n<code><code><span ><span >\/\/ Using the array function for a regular array\r\n<\/span><span >$fruit <\/span><span >= array(<\/span><span >'apple'<\/span><span >,<\/span><span >'grape'<\/span><span >,<\/span><span >'orange'<\/span><span >);<\/span><\/span><\/code><\/code>\r\n\r\n<code><code><span ><span >\/\/ Using the array function for an associative array\r\n<\/span><span >$mother <\/span><span >= array(<\/span><span >'kitten' <\/span><span >=&gt; <\/span><span >'cat'<\/span><span >, <\/span><span >'puppy' <\/span><span >=&gt; <\/span><span >'dog'<\/span><span >);<\/span><\/span><\/code><\/code>\r\n\r\n<code><span ><span >\/\/ The following line prints \"Bob's cat ate an orange.\"\r\n<\/span><span >echo <\/span><span >\"<\/span><span >$person<\/span><span >'s <\/span><span >$mother[<\/span><span >'kitten'] ate an <\/span><span >$fruit[2]<\/span><span >.\"<\/span><span >;\r\n<\/span><span >?&gt; <\/span>\r\n<\/span>\r\n<\/code>\r\n\r\n<\/div>\r\nOne of the most powerful features of PHP is its easy access to requested variables. For instance, consider the following short form:\r\n<div class=\"codeblock\"><code><span >\r\n<span >&lt;<\/span><span >form action<\/span><span >=<\/span><span >\"form_handler.php?posted=true\" <\/span><span >method<\/span><span >=<\/span><span >\"post\"<\/span><span >&gt;\r\n&lt;<\/span><span >input name<\/span><span >=<\/span><span >\"formvar\"<\/span><span >&gt;\r\n&lt;<\/span><span >input value<\/span><span >=<\/span><span >\"Submit Query\" <\/span><span >type<\/span><span >=<\/span><span >\"submit\"<\/span><span >&gt;\r\n&lt;\/<\/span><span >form<\/span><span >&gt; <\/span>\r\n<\/span>\r\n<\/code><\/div>\r\nOn submitting the form, the value of the variable \u201cformvar\u201d will be available to the form_handler.php script simply as\r\n<div class=\"codeblock\"><code><span >\r\n<span >$_POST[<\/span><span >'formvar'<\/span><span >] <\/span>\r\n<\/span>\r\n<\/code><\/div>\r\nand the variable posted simply\r\n<div class=\"codeblock\"><code><span >\r\n<span >$_GET[<\/span><span >'posted'<\/span><span >]<\/span><span >. <\/span>\r\n<\/span>\r\n<\/code><\/div>\r\nThis is true for all POST and GET form calls."},"excerpt":{"rendered":"<p>One of the most powerful features of PHP is its easy access to requested variables. These variables are assigned in a typical manner.  <\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"lsx_disable_title":"0","_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","footnotes":""},"categories":[179,166],"tags":[17159],"topics":[19781],"class_list":["post-1659","post","type-post","status-publish","format-standard","hentry","category-php","category-website","tag-variables","topics-php"],"acf":[],"additional_meta":{"category_title":[{"term_id":179,"name":"PHP","slug":"php","term_group":0,"term_taxonomy_id":179,"taxonomy":"category","description":"Allowing you to create dynamic content that interacts with databases","parent":168,"count":20,"filter":"raw","term_order":"97","cat_ID":179,"category_count":20,"category_description":"Allowing you to create dynamic content that interacts with databases","cat_name":"PHP","category_nicename":"php","category_parent":168},{"term_id":166,"name":"Website","slug":"website","term_group":0,"term_taxonomy_id":166,"taxonomy":"category","description":"About your Website(s)","parent":0,"count":169,"filter":"raw","term_order":"120","cat_ID":166,"category_count":169,"category_description":"About your Website(s)","cat_name":"Website","category_nicename":"website","category_parent":0}],"tag_title":[{"term_id":17159,"name":"Variables","slug":"variables","term_group":0,"term_taxonomy_id":17159,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw","term_order":"2393"}]},"featured_image_src":null,"author_info":{"display_name":"marketing","author_link":"https:\/\/xneelo.co.za\/help-centre\/author\/marketing\/","author_avatar":"https:\/\/secure.gravatar.com\/avatar\/a6ea315e112423b2b955cb020fbce2b0835956c6ad85ff0f13f1db298977eaaa?s=96&d=mm&r=g"},"_links":{"self":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts\/1659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/comments?post=1659"}],"version-history":[{"count":0,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts\/1659\/revisions"}],"wp:attachment":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/media?parent=1659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/categories?post=1659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/tags?post=1659"},{"taxonomy":"topics","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/topics?post=1659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}