Learn PHP variables, variable scope, Global variables, this variable
Apr 8th
PHP Variables:
Variables are used for storing values.Variables in PHP start with a $ (dollar sign). They can be used to store numbers, strings or arrays. Once the variable is set, it can be used multiple times.PHP manages it automatically if you not specify variable data type.
Variables in PHP are case sensitive and by default are assigned by value. Variable initialization is not mandatory.
PHP also has some predefined variables. Different categories of variables include:
$GLOBAL: References all variables available in global scope
$_GET — HTTP GET variables
$_POST — HTTP POST variables
$_SESSION — Session variables
$_REQUEST — HTTP Request variables
The above variables are also called super variables, as they have global scope; the scope will b define in below paragraph lines.
Example:
<?php
$sample =”Sample text”;
<?
Registration of Variables into Session:
By using the session_register() function, global variables in PHP can be registered. This function accepts different number of arguments, ( either variable that has string or an array consisting of variable names or other arrays).
Example:
Session_register(“smple”);
$_session can also be used for registering variables.
Example:
$_SESSION['count'] = 0;
Variable Scope in Functions in PHP:
The scope of a variable is determined based on the context in which is defined. i.e. local or global. In PHP global variables must be declared global inside a function if they are going to be used in that function.
Example: The script below will not produce any result because “x” ‘s scope is outside the function. If the variable is declared as global, it can be used from anywhere in the script.
<?php
$x = 10; //global
Function sample()
{
Echo $x;
}
Test();
?>
Describe $_ENV and $_SERVER:
- $_ENV is the array under Superglobal variables. They are he environment variables. These variables are imported into PHP’s global namespace from the environment of PHP parser.
Example:
<?php
Echo ‘my user name is ‘ .$_ENV[“USER”] ‘!’;
?>Output:
My user name is sample ! - $_SERVER is the array under Superglobal variables. They are array containing information about headers, paths and script locations. Web server creates entries of this array.
Example:
<?php
Echo $_SERVER [‘SERVER_NAME’];
?>
About “this” Variable:
In order to specify that you are working with local variables within a function $this variable is used. Inside a function of an object, PHP automatically sets the $this variable contains that object – we need not to do anything to have access to it.
Example:
Function person()
{
Print “{$this->age}”;
}
PHP Advantages, PHP Benifits
Apr 8th
PHP advantages
- PHP is offering a variety of security mechanisms.
- it is a better choice of modular programming as it has easy connectivity abilities.
- PHP has ability to easily be interfaced with a variety of libraries, xml’s.
- Open source software makes it best attractive choice for every one.
- Easy to remember the syntax, syntax is like to C and C++.
- Easily can create,manage dynamic web pages with PHP.
- PHP People Community, a good place where people can make their scripts and post relating to the requirement.
PHP Advantage over Java:
- PHP: easy and simple language built for web applications
- PHP: open source and customization becomes convenient.
- PHP: easily integrates with variety of database, both open-source (MySQL, PostgreSQL, SQLite) and commercial (Oracle, MS SQL Server); while Java relies on JDBC drivers for database connectivity.
50 Free High Quality Web Templates
Mar 10th
http://www.noupe.com/css/50-free-high-quality-and-new-xhtmlcss-templates.html
Guess Gender ;-)
Nov 22nd
Just a simple function to demonstrate the use of a few very simple but useful functions of PHP. This function simply accepts a parameter ($relation) and returns the “Gender (sex)” of the relationship.
Here’s the function:
<?php
function guessGender($relation)
{
$gen = "";
$relation = ucwords(strtolower($relation));
$relations = array(
"Male" => array("Husband", "Son", "Father", "Brother"),
"Female" => array("Wife", "Daughter", "Mother", "Sister")
);
foreach($relations as $gender => $relationType){
if(in_array($relation, $relationType)){
$gen = $gender;
}
}
return $gen;
}
?>
For the ease of your understanding, I have linked each PHP function, language construct and other statements to Official PHP manual. Simply click on the provided links (in above code) to see the detailed information on each.
Problem and Solution!
Nov 18th
I found an interesting problem on net and as a noivce decided to solve it all by self with the help of PHP.
Problem:”If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
”
Solution:
“<?php
for($counter=1; $counter < 1000 ; $counter ++)
{
//echo ”
“;
//echo ”
“.$counter.”–> “;
$int_var1 = ($counter/3); //current counter will be divided by 3
$int_var2 = ($counter/5); //current counter will be divided by 5
$str_type1 = gettype($int_var1); //This will return integer if counter was divided by 3 properly
$str_type2 = gettype($int_var2); //This will return integer if counter was divided by 5 properly
if ($str_type1 == ‘integer’ || $str_type2 == ‘integer’ ) //to check which “counter” was properly divided
{
//echo “This is integer”;
$int_result = $int_result+$counter;
}
else
{
//echo ” NON INTEGER” ;
}
}
echo “Result is: $int_result “;
?>
“
Simple File Browser with Pictures Explorer
Nov 6th
This simple file browser, written in PHP, is a very useful library of functions. This library provides basic functionality of file and directory browsing along with many configurable options.
However this is a basic version, and I just wrote it to use at a client’s website. I will be extending it more to provide other useful options like Delete, Move, Copy, Rename and etc.
Currently, it provides listing of directories and files. Navigation thru directories, opening files. You can also use it as an Image/Picture Explorer. There’s a file “fb.php” included which demonstrate the usage at front-end. Following files are included in this package:
- fb.config.php: the configuration file
- fb.lib.php: the main library file, contains all functions
- fb.css: the sample style sheet
- fb.php: the sample usage file
- images folder: includes icons for file types
Download source code here File Browser Source
Web Designs
Nov 4th
Ready made designs, contributed from our members and from different volunteer resources. Information on good designs, techniques and many more…
Recent Comments