PHP Variables
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Rules for PHP variables:
Example
<?php
$txt = "India is the great country.";
echo "I write $txt!";
?>
PHP is a Loosely Typed Language
<?php
$x = 5; // global scope
function myTest()
{
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
PHP The global Keyword
<?php
$x = 5;
$y = 10;
function myTest()
{
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
Example
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
<?php
function myTest()
{
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
PHP echo and print Statements
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
The print statement can be used with or without parentheses: print or print().
Example
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
The following example shows how to output text and variables with the print statement:
<?php
$txt1 = "Learn PHP";
$txt2 = "PKS";
$x = 5;
$y = 4;
print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
Constants
define(name, value, case-insensitive)
Parameters:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
1. <?php
define("A", "Welcome to shahblogs.com!");
echo A;
?>
2. <?php
define("GREETING", "Welcome to shahblogs.com!", true);
echo greeting;
?>
3. <?php
define("GREETING", "Welcome to shahblogs.com!");
function myTest()
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Eg-:<?php$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Rules for PHP variables:
- A variable starts with the $ sign, followed by the name of the variable
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive ($age and $AGE are two different variables)
Example
<?php
$txt = "India is the great country.";
echo "I write $txt!";
?>
PHP is a Loosely Typed Language
In the example above, notice that we did not have to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on its value. In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it.
PHP has three different variable scopes:- local
- global
- static
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
Example<?php
$x = 5; // global scope
function myTest()
{
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
Example<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
PHP The global Keyword
The global keyword is used to access a global variable from within a function. To do this, use the global keyword before the variables (inside the function):
Example<?php
$x = 5;
$y = 10;
function myTest()
{
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
The example above can be rewritten like this:Example
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable
Example<?php
function myTest()
{
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
PHP echo and print Statements
echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print. The echo statement can be used with or without parentheses: echo or echo().
Example<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
The print statement can be used with or without parentheses: print or print().
Example
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
The following example shows how to output text and variables with the print statement:
<?php
$txt1 = "Learn PHP";
$txt2 = "PKS";
$x = 5;
$y = 4;
print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
Constants
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Note: Unlike variables, constants are automatically global across the entire script.
Syntaxdefine(name, value, case-insensitive)
Parameters:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
1. <?php
define("A", "Welcome to shahblogs.com!");
echo A;
?>
2. <?php
define("GREETING", "Welcome to shahblogs.com!", true);
echo greeting;
?>
3. <?php
define("GREETING", "Welcome to shahblogs.com!");
function myTest()
0 comments
Post a Comment