<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gunawan Efendi</title>
    <description>The latest articles on DEV Community by Gunawan Efendi (@gunawanefendi).</description>
    <link>https://dev.to/gunawanefendi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1465295%2F660622fd-4c1d-446f-b3b8-3436065d69bd.jpeg</url>
      <title>DEV Community: Gunawan Efendi</title>
      <link>https://dev.to/gunawanefendi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gunawanefendi"/>
    <language>en</language>
    <item>
      <title>Variable &amp; Variable Scope | PHP Fundamentals</title>
      <dc:creator>Gunawan Efendi</dc:creator>
      <pubDate>Sun, 30 Jun 2024 16:33:30 +0000</pubDate>
      <link>https://dev.to/gunawanefendi/variable-variable-scope-in-php-3l4a</link>
      <guid>https://dev.to/gunawanefendi/variable-variable-scope-in-php-3l4a</guid>
      <description>&lt;h2&gt;
  
  
  Create Variable in PHP
&lt;/h2&gt;

&lt;p&gt;The rules when you create variables in PHP:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variable declaration with dollar ($) followed by variable name&lt;/li&gt;
&lt;li&gt;Variable name must start with a letter or underscore (_)&lt;/li&gt;
&lt;li&gt;Variable name is case-sensitive&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Valid variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$name = "Gunawan"; //valid
$Name = "Gunawan"; //valid
$_name = "Gunawan; //valid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not valid variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$4name = "Gunawan"; //not valid
$user-name = "Gunawan"; //not valid
$this = "Gunawan"; //not valid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variable Scope
&lt;/h2&gt;

&lt;p&gt;PHP has 3 variable scopes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Global&lt;/li&gt;
&lt;li&gt;Local&lt;/li&gt;
&lt;li&gt;Static&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Global scope
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$name = "Gunawan";

function get_name() {
echo $name; // not valid
}

get_name();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access a global variable within a function you must declare a global variable with the keyword 'global' within a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$name = "Gunawan";

function get_name() {
global $name;
echo $name; // valid
}

get_name();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use Array GLOBALS to Access Global Variable
&lt;/h2&gt;

&lt;p&gt;The second way to access global variables is to use a global array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$name = "Gunawan";

function get_name() {
echo $GLOBALS['name']; // valid
}

get_name();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Static Variable
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function test() {
static $number = 0;
echo $number;
$number++;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variable Super Global in PHP:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;$GLOBALS&lt;/li&gt;
&lt;li&gt;$_SERVER&lt;/li&gt;
&lt;li&gt;$_GET&lt;/li&gt;
&lt;li&gt;$_POST&lt;/li&gt;
&lt;li&gt;$_FILES&lt;/li&gt;
&lt;li&gt;$_COOKIE&lt;/li&gt;
&lt;li&gt;$_SESSION&lt;/li&gt;
&lt;li&gt;$_REQUEST&lt;/li&gt;
&lt;li&gt;$_ENV&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Download my repository php fundamental from &lt;a href="https://github.com/gugunefendi/php-fundamental"&gt;my github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
