Variables Scope in PHP : Explained in easy language

Variables Scope in PHP

There are three types of variable scopes in PHP .  They are as follows:

1)      Local

2)      Global

3)      Static

Local Scope : When a variable declared inside a function (has a local scope ) and it can only be accessed within that function, the variable is called as local scope variable .

E.g.

<?php

function localvar()

{

$x=6; //local scope variable

echo "Variable x inside function is : $x";

}

localvar();

echo "Variable x outside function is : $x";//generate an error like undefined variable

?>

 

Output:  

Variable x inside function is : 6

Notice: Undefined variable: x

Variable x inside function is :

 

Global Scope : When a variable declared outside a function (has a global scope) and it can only be accessed outside a function, the variable is called as global scope variable .

E.g.

 <?php

$x=10; //global scope

function globalvar()

{

echo "Variable x inside function is : $x";//generate an error like undefined variable

}

globalvar();

echo "Variable x outside function is : $x";

?>

Output:

Notice: Undefined variable x

Variable x inside function is :

Variable x outside function is : 10

Global Keyword : By using global keyword we can access a global variable within a function. Just add or write global keyword before the variables inside the function. As follows,

e.g.

<?php

$x=10; //global scope

function globalvar()

{

            global $x;//By using global keyword

echo "Variable x inside function is : $x<br>";

}

globalvar();

echo "Variable x outside function is : $x";

?>

Output :

Variable x inside function is : 10

Variable x outside function is : 10

 

Static Scope : When a function is executed , all of its variables are deleted. However we want a local variable not to be deleted. We need it for further job. Use the static keyword when you first declared the variable . Then each time the function is called , the variable will still have the information it contained from the last time the function called.

e.g.

<?php

function scopevar (){

            static $x=11;

            echo $x;

            $x++;

}

scopevar ();

echo "<br>";

scopevar();

echo "<br>";

scopevar ();

?>

Output :

11

12

13

     

              In that way we learn variables scope in short .

Note: Read this blog regularly for important concepts and interview questions.

This blog is useful for IT students, IT freshers, BCA/MCA freshers. This blog contains Resume Tips, interview questions with answers.

PHP Interview questions with answers for Freshers - 1

Many interview questions we already listed on this blog, please read through out the posts related to PHP interview questions.


Q. What is the use of "echo" in php?
Answer: It is used to print a data in the webpage,
Example: <?php echo 'Welcome to Technology Park'; ?>
It will print the output or text on webpage as:
Welcome to Technology Park

Q. How to include a file to a php page?
VERY IMPORTANT INTERVIEW QUESTION
Answer: We can include a file using "include() " or "require()" function with file path as its parameter.

Q. How stop the execution of a php scrip ?
Answer: exit() function is used to stop the execution of a page.
At any point of script you can use the exit or exit() statement.

example:
<?php
echo "Welcome to Technology Park";
exit();
echo "not printed";
?>

Output:
Welcome to Technology Park

Q. How do you define a constant?
Answer:Using define() directive, like define ("MYCONSTANT",150)


Keep visiting the blog regularly for Latest Interview questions and Resume Tips.

PHP program to check whether the given number is palindrome or not

This PHP program is very useful for IT freshers.

Most of the IT companies ask this program in technical interview.




PHP program to check whether the given number is palindrome or not.

<?php
// PHP program to check whether the given number is palindrome or not
$n=123;
$rev=0;
$n1=$n;
while((int)$n > 0){
$rem = $n % 10;
$rev = ($rev*10) + $rem;
$n = (int)$n / 10;
}
echo "Reverse of $n1 = ".$rev."<br/>";
if($n1 == $rev){
echo "$n1 is palindrom number";
}else{
echo "$n1 is not palindrom number";
}
?>

This blog is very useful for IT Freshers.
Use right side menus to read useful posts like Resume Tips, Interview questions, Jobs, Programming, etc.

Other important Posts:
How to find PHP version through command prompt in windows? 

Important PHP string functions


PHP program to check whether the given number is prime or not

This post is very important for IT Students and IT freshers.

The most important program asked in various interviews or technical tests:
Write a PHP program to check whether the given number is prime or not.




What is Prime number: the number which is divisible by 1 and itself only.


So, here is the Program to check whether the given number is prime or not.

<?php
// What is Prime number: the number which is divisible by 1 and itself only.
$n=6; // The number which we want to check if it is PRIME or NOT PRIME
$prime=true;
for($i=2;$i<$n;$i++){
if($n%$i == 0){
$prime = false; // If Number is divisible by the $i iteration, it is not prime
break;
}
}
if($prime){
echo "PRIME";
}else{
echo "NOT PRIME";
}

?>

Output:
NOT PRIME

// when we assign value to n is 5 then output will be PRIME
--------------------------------------------------------------------------------------------




Other important posts for students of BCA/MCA/MCM/Msc/Bsc/MBA-IT/BE/BTech

Guidance for IT Students

Future of PHP Developer

PHP: Past, Today and Future

This blog is very helpful for IT Students and IT Freshers.


Database connection using PHP MySqli

For PHP freshers, one important thing is mysql_* functions are deprecated. Instead of mysql_* functions now we use mysqli_* functions.

PHP mysqli_connect() Function is used for connection to mysql database.




Example
Open a new connection to the MySQL server:

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>


If we are using wamp or xamp on localhost,
my_user = root

my_password = "" (blank/empty)
my_db = your database name


This function returns an object representing the connection to the MySQL server, the returned connection object is used for executing mysql queries.


Insert Data Into MySQL Using MySQLi:
The INSERT INTO statement is used to add new records to a MySQL table:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

Example:
The following examples add a new record to the "MyGuests" table:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>


In this way, we just understood the concept and basics of mysqli connection.