{"id":14987,"date":"2020-06-17T09:11:15","date_gmt":"2020-06-17T09:11:15","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=14987"},"modified":"2022-04-28T09:15:24","modified_gmt":"2022-04-28T09:15:24","slug":"what-is-variable-in-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-is-variable-in-php\/","title":{"rendered":"How to Define a Variable in PHP"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong>what is variable in <\/strong>PHP<\/h4>\n\n\n\n<p>Variables are used to keep data in one place. Such as string of text numbers, etc. The value of a variable can change on the browser of a script. Here are some important things to know about variables.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.<\/li><li>After declaring a variable it can be reused throughout the code.<\/li><li>The assignment operator (=) used to assign value to a variable.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Creating PHP Variables<\/h4>\n\n\n\n<p>A variable ($) in the first sentence starts with this symbol, followed by the name of Valiabale,<br>As given in the example below<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/1c4df98e337570765b08351e304e7351.js\"><\/script>\n\n\n\n<p>After the first given above, the value of $ txt will be Hello World! The value of the next $ X will be 5, and the value of the next $ y will be 10.5<\/p>\n\n\n\n<p><strong>Note<\/strong>: When we assign a text value to a variable, we must put a quote around the value.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">PHP Variables<\/h4>\n\n\n\n<p>A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).<\/p>\n\n\n\n<p><strong>Rules for PHP variables:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A variable starts with the $ sign, followed by the name of the variable<\/li><li>A variable name must start with a letter or the underscore character<\/li><li>A variable name cannot start with a number<\/li><li>A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )<\/li><li>Variable names are case-sensitive ($age and $AGE are two different variables)<\/li><\/ul>\n\n\n\n<p><strong>Output Variables<\/strong>&#8211;<\/p>\n\n\n\n<p>                The PHP echo statement is often used to output data to the screen.<\/p>\n\n\n\n<p>The example. I have given below is how to output the variable<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/291c02504b6a30e690805574ab69cf4a.js\"><\/script>\n\n\n\n<p>The following example will output the sum of two variables:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/528f7d66fc9896cb3c8169bd8f6b3d41.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">PHP Variables Scope<\/h4>\n\n\n\n<p>In PHP, variables can be declared anywhere in the script.<\/p>\n\n\n\n<p>The scope of a variable is the part of the script where the variable can be referenced\/used.<\/p>\n\n\n\n<p>PHP has three different variable scopes:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>local<\/strong><\/li><li><strong>global<\/strong><\/li><li><strong>static<\/strong><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">local scope<\/h4>\n\n\n\n<p>A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/99bc89dc66e71ba37f5b54b562d8f7c5.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">Global Scope<\/h4>\n\n\n\n<p>A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/3a8cfbee970e43fde9cacb5f1b697a00.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">Static Keyword:<\/h4>\n\n\n\n<p>Generally, when we write a function completely, all its variables are removed.Sometimes we want no local variable to be removed, we need static for further work,<\/p>\n\n\n\n<p>When we first declare a variable, in this case we use the static keyword<\/p>\n\n\n\n<p>As i explained in the example below<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/e93592210205695efc2d51ebc4a95300.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_40828\"><iframe loading=\"lazy\"  id=\"_ytid_62556\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_40828\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>what is variable in PHP Variables are used to keep data in one place. Such as string of text numbers, etc. The value of a variable can change on the&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[],"class_list":["post-14987","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=14987"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14987\/revisions"}],"predecessor-version":[{"id":24757,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14987\/revisions\/24757"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=14987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=14987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=14987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}