{"id":377,"date":"2018-07-14T00:51:48","date_gmt":"2018-07-14T00:51:48","guid":{"rendered":"https:\/\/www.go4expert.com\/tutorials\/?p=377"},"modified":"2018-08-02T01:23:15","modified_gmt":"2018-08-02T01:23:15","slug":"php-variables","status":"publish","type":"post","link":"https:\/\/www.go4expert.com\/tutorials\/php-variables\/","title":{"rendered":"PHP Variables"},"content":{"rendered":"<p>Variables are placeholders for information to facilitate the programmer with named storage to be used in the program. They play an important role in a program because they enable us to store, reference and manipulate information in a readable way throughout the lifetime of a program.<\/p>\n<p>In PHP variable names always begin with a $ sign followed by an alphanumeric name.<\/p>\n<p><b>Example :<\/b><br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$msg = \"Learn PHP because it is the best\";\n$number1 = 30;\n$number2 = 50;\n?&gt;<\/textarea><\/div><\/p>\n<p>In the above example the variable $msg holds the value \"Learn PHP because it is the best\", the variable $number 1 holds the value 30 and $number2 holds the value 50.<\/p>\n<p>It can be observed in the above example that the variables were never declared. This is because PHP does not require explicit declaration of variables and their types. Simply using a variable implies that a variable has been created and its datatype is assumed from the initial value that it is assigned.<\/p>\n<h2>Naming Convention for PHP variables<\/h2>\n<p>PHP requires us to follow some rules while naming a variable:<\/p>\n<ol>\n<li>Variable names always begin with a $ sign followed by the name itself.<\/li>\n<li>The name could either be a short name or it could be a longer descriptive name.<\/li>\n<li>Variables usually have descriptive names making the program more intuitive and meaningful.<\/li>\n<li>Variable names need not be declared and no explicit data type of the variable needs to be mentioned. Hence PHP assumes the datatype of a variable based on its initial assignment.<\/li>\n<li>Variable names can consist of alphanumeric characters and underscore (A-z, 0-9, and _ ).<\/li>\n<li>Variable names always start with an alphabet or an underscore and cannot begin with a number.<\/li>\n<li>PHP variable names are always case-sensitive. Eg $name is different from $NAME and they are both 2 separate variables.<\/li>\n<\/ol>\n<h2>PHP \u2013 A Loosely Typed Language<\/h2>\n<p>Languages like C, C++, Java require that the programmer explicitly declare variables and their datatypes. Although we can declare a variable in PHP it is not mandatory. We can start using a variable directly without declaring it, because of which it is called a loosely typed language.<\/p>\n<p>Whenever we use a variable in PHP without declaring it PHP converts it to the correct datatype by looking at the value that the variable is initially assigned.<\/p>\n<p><b>Example:<\/b><br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$msg = \"I love to program in PHP because it is easy\";\necho $msg;\n?&gt;<\/textarea><\/div><\/p>\n<p>Here the variable $msg is assumed to be of type String because it is assigned a string value.<\/p>\n<h2>Output a Variable<\/h2>\n<p>There are several ways to output the value of a variable during the execution of a program. The \u2018Echo\u2019 command is one of the most common ways to output the value of a variable during program execution and because of this we will be using it for all our examples. Using the \u2018Echo\u2019 command we can output variables of any datatype.<\/p>\n<p><b>Example : <\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$msg = \"Learn PHP because it is popular\";\necho $msg;\n?&gt;<\/textarea><\/div>\n<p>The above example outputs a string<br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$x = 30;\n$y = 40;\necho $x + $y;\n?&gt;<\/textarea><\/div><br \/>\nThe above example outputs a number.<\/p>\n<h2>Variable Scope in PHP<\/h2>\n<p>The scope of a variable refers to the portions of the program where we can eference the variable. Hence the scope of a variable is also known as the visibility of the variable.<\/p>\n<p>PHP allows variables to be declared anywhere in the program. Depending on their visibility variables can have 3 different scopes of local, global and static.<\/p>\n<h3>Global Scope<\/h3>\n<p>A variable having a global scope is visible or accessible throughout the program outside functions. Hence it is called a global variable.<\/p>\n<p>It gets hidden inside a function because of which we can create variables inside a function which have the same name as the global variable.<br \/>\n<b>Example :<\/b><br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">\n&lt;?php\r\n$test_var = 15; \/\/ global scope\r\n\r\nfunction myTest() {\r\n    echo \"Variable test_var inside function is: $test_var\";\r\n    \/* using x inside this function will generate an error echo \"Variable test_var inside function is: $test_var\";*\/\r\n    \/* here the output will be \u201cVariable test_var inside function is:\u201d because the value of test_var has not been set inside the function*\/\r\n}\r\nmyTest();\r\n\r\necho \"Variable test_var outside function is: $test_var\";\r\n?&gt;\n<\/textarea><\/div><\/p>\n<p>The output will be<\/p>\n<p>Variable test_var inside function is:<br \/>\nVariable test_var outside function is:15<\/p>\n<p>Because the value of test_var has been set outside of the function and so it is not available inside the function unless we explicitly define it inside the function using the global keyword as below.<\/p>\n<h3>Global Keyword<\/h3>\n<p>The global keyword is used to access global variables inside a function.<\/p>\n<p><b>Example :<\/b><br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">\n&lt;?php\r\n$test_var = 15; \/\/ global scope\r\n\r\nfunction myTest() {\r\n    global $test_var; \/\/ We can now use the global variable inside the function.\r\n    echo \"Variable test_var inside function is: $test_var\";\r\n}\r\nmyTest();\r\n\r\necho \"Variable test_var outside function is: $test_var\";\r\n\/\/ here the output will be \u201cVariable test_var inside function is:15\u201d because the value of test_var has been set inside the function\r\n?&gt;\n<\/textarea><\/div><\/p>\n<p>The output will be<\/p>\n<p>Variable test_var inside function is:15<br \/>\nVariable test_var outside function is:15<\/p>\n<h3>Global Variable<\/h3>\n<p>All the global variables are stored in an array called the $GLOBALS[index]. Here index is the name of the variable. We can access and update global variables inside functions using this array.<\/p>\n<p><b>Example :<\/b><br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">\n&lt;?php\r\n$no1 = 25;\r\n$no2 = 10;\r\n\r\nfunction myTest() {\r\n    $GLOBALS['sum'] = $GLOBALS['no1'] + $GLOBALS[' no2'];\r\n    \/\/ access the global variables of no1, no2, sum by using the global array.\r\n}\r\n\r\nmyTest();\r\necho $sum; \/\/ outputs 35\r\n?&gt;\n<\/textarea><\/div><\/p>\n<h3>Local Scope<\/h3>\n<p>Any variable which is declared within a function is called a local variable. Local variables have local scope. They can be accessed and updated only within the function that they are declared in.<\/p>\n<p><b>Example :<\/b><br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">\n&lt;?php\r\nfunction func_local() {\r\n    $local_variable = 5; \/\/ local scope\r\n    echo \"Variable which is local inside function is: $local_variable\";\r\n}\r\nmyTest();\r\n\r\n\/\/ using $local_variable outside the function will generate an error\r\necho \"Variable local_variable outside function is: $local_variable\";\r\n?&gt;\r\n\n<\/textarea><\/div><\/p>\n<p>The output will be \"Variable local_variable outside function is:\" as $local_variable does not exist outside.<\/p>\n<h2>The Static Keyword and Static Variables<\/h2>\n<p>Variables which are local to a function lose their value once the function execution is over. However, sometimes we need to retain the value of the variables throughout the life of the program even after the function execution.<\/p>\n<p>Hence, we can declare the function value to be static using the STATIC keyword. A static variable is only accessible inside the function i.e. it still has local scope only even though its value is retained after the function completes execution.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\nfunction func1() {\r\n    static $no1 = 1;\r\n    $no2 = 1;\r\n    echo $no1 . $no2;\r\n    $no1++;\r\n    $no2++;\r\n}\r\n\r\nfunc1(); \/\/output will be 1 and 1 because $no1 and $no2.\r\nfunc1(); \/\/output will be 2 and 1 because $no1 is incremented by 1 in the previous call and is static but $no2 doesn't persist as it was normal variable with local scope\r\nfunc1(); \/\/output will be 3 and 1 because $no1 incremental value is being persisted.\r\n?&gt;<\/textarea><\/div><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Learn about the rules of usage of variables in PHP . PHP scope types ,variable naming and usage rules are covered exhaustively.<\/p>\n","protected":false},"author":6,"featured_media":210,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":{"0":"post-377","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-php","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Variables - Go4Expert Tutorials<\/title>\n<meta name=\"description\" content=\"Learn about the rules of usage of variables in PHP . PHP scope types ,variable naming and usage rules are covered exhaustively.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.go4expert.com\/tutorials\/php-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Variables - Go4Expert Tutorials\" \/>\n<meta property=\"og:description\" content=\"Learn about the rules of usage of variables in PHP . PHP scope types ,variable naming and usage rules are covered exhaustively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.go4expert.com\/tutorials\/php-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"Go4Expert Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-14T00:51:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-02T01:23:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"770\" \/>\n\t<meta property=\"og:image:height\" content=\"385\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"vnlaks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"vnlaks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-variables\/\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/php-variables\/\",\"name\":\"PHP Variables - Go4Expert Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\"},\"datePublished\":\"2018-07-14T00:51:48+00:00\",\"dateModified\":\"2018-08-02T01:23:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae\"},\"description\":\"Learn about the rules of usage of variables in PHP . PHP scope types ,variable naming and usage rules are covered exhaustively.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.go4expert.com\/tutorials\/php-variables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.go4expert.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/\",\"name\":\"Go4Expert Tutorials\",\"description\":\"Programming And Web Development Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae\",\"name\":\"vnlaks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g\",\"caption\":\"vnlaks\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Variables - Go4Expert Tutorials","description":"Learn about the rules of usage of variables in PHP . PHP scope types ,variable naming and usage rules are covered exhaustively.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.go4expert.com\/tutorials\/php-variables\/","og_locale":"en_US","og_type":"article","og_title":"PHP Variables - Go4Expert Tutorials","og_description":"Learn about the rules of usage of variables in PHP . PHP scope types ,variable naming and usage rules are covered exhaustively.","og_url":"https:\/\/www.go4expert.com\/tutorials\/php-variables\/","og_site_name":"Go4Expert Tutorials","article_published_time":"2018-07-14T00:51:48+00:00","article_modified_time":"2018-08-02T01:23:15+00:00","og_image":[{"width":770,"height":385,"url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","type":"image\/jpeg"}],"author":"vnlaks","twitter_card":"summary_large_image","twitter_misc":{"Written by":"vnlaks","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.go4expert.com\/tutorials\/php-variables\/","url":"https:\/\/www.go4expert.com\/tutorials\/php-variables\/","name":"PHP Variables - Go4Expert Tutorials","isPartOf":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#website"},"datePublished":"2018-07-14T00:51:48+00:00","dateModified":"2018-08-02T01:23:15+00:00","author":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae"},"description":"Learn about the rules of usage of variables in PHP . PHP scope types ,variable naming and usage rules are covered exhaustively.","breadcrumb":{"@id":"https:\/\/www.go4expert.com\/tutorials\/php-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.go4expert.com\/tutorials\/php-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.go4expert.com\/tutorials\/php-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.go4expert.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"PHP Variables"}]},{"@type":"WebSite","@id":"https:\/\/www.go4expert.com\/tutorials\/#website","url":"https:\/\/www.go4expert.com\/tutorials\/","name":"Go4Expert Tutorials","description":"Programming And Web Development Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae","name":"vnlaks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g","caption":"vnlaks"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9JSSi-65","_links":{"self":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":8,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":424,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/377\/revisions\/424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}