Variables In PHP
*We cannot access the global variables from function. To access the global variables wecan use $GLOBALS (or) initialize the variable with in function as global.
EX:
<?php
error_repoting(E_ALL);
$x=100;
function dan()
{
$x="Danphp";
$sno=1234;
echo $sno;
echo $GLOBAL['x'];
echo $x;
}
function php()
{
global $x;
$x="dan";
echo $x;
}
dan()
php()
?>
error_repoting(E_ALL);
$x=100;
function dan()
{
$x="Danphp";
$sno=1234;
echo $sno;
echo $GLOBAL['x'];
echo $x;
}
function php()
{
global $x;
$x="dan";
echo $x;
}
dan()
php()
?>
Variable Variables: If we assign variable name as a value name another variable comes under the consept.
EX:
<?php
$x="hello";
$y="x";
echo $$y;
?>
$x="hello";
$y="x";
echo $$y;
?>
Static Variable: Static variable can remember the previous static keyword, is used to maintain the state of the application.
EX:
<?php
function fun1()
{
static $x=100;
$x++;
echo $x;
}
fun1();
fun1();
fun1();
fun1();
?>
function fun1()
{
static $x=100;
$x++;
echo $x;
}
fun1();
fun1();
fun1();
fun1();
?>
Reference Variables: A reference variable is an alias name of actual variable. Both variables reference the same memory location.
<?php
$x=100;
$y=&$x;
echo $y;
echo $x;
$y=200;
echo $x;
?>
$x=100;
$y=&$x;
echo $y;
echo $x;
$y=200;
echo $x;
?>
Super Global Variables: Super global variables are special type of variables in php. We can access those variables from any location. Within the script also we can access from another pages within the application.
Types: $_GET, $_POST, $_SERVER, $_REQUEST, $_SESSION, $_FILES, $_ENV, $_COOKIE.
Super Global Variables
i) $_GET: By using these variables we hold the values of get method.
EX:
Danphp.php
<form action="page1.php" method="get">
<input type="text" name="txt1">
<br>
<input type="text" name="txt2">
<br>
<input type="submit" value="CLICK" />
</form>
Page1.php
Welcome to Page1
<?php
$_GET['txt1'];
$_GET['txt2'];
print_r($_GET);
?>
ii) $_POST: This super global variable is used to hold the posted values of post method.
Login.php:
<form action="var.php" method="post">
username:<input type="text" name="txt1" value="<?php echo $dan?>">
<br />
password:<input type="text" name="txt2" value="<?php echo $php?>">
<br>
<input type="submit" value="click" name="sub">
</form>
username:<input type="text" name="txt1" value="<?php echo $dan?>">
<br />
password:<input type="text" name="txt2" value="<?php echo $php?>">
<br>
<input type="submit" value="click" name="sub">
</form>
Var.php
<?php
if(isset($_POST['sub']));
{
$dan=$_POST['txt1'];
$php=$_POST['txt2'];
if($dan=="dinesh" and $php=="dinesh123")
{
echo "valid";
}
else
echo "invalid";
}
?>
if(isset($_POST['sub']));
{
$dan=$_POST['txt1'];
$php=$_POST['txt2'];
if($dan=="dinesh" and $php=="dinesh123")
{
echo "valid";
}
else
echo "invalid";
}
?>
$_REQUEST: By using this super global variable we can get the values of get method and post method and query string (cookies) along with cookie value.
Query string: Query string is small amount of data on the url address bar followed by (?).
*We can pass the query string in two ways.
1.Query string with name and value.
2.Query string with value only.
2.Query string with value only.
@When we are passing the query string, we must use the post method. Because the get method overrides the query string with control values.
EX:
form.php
<form method="get" action="page1.php? city=hyd">
<input type="text" name="txt1">
<br>
<input type="submit" name="sub" value="click">
</form>
<input type="text" name="txt1">
<br>
<input type="submit" name="sub" value="click">
</form>
Page1.php
<?php
print_r($_REQUEST);
echo $_REQUEST['city'];
?>
print_r($_REQUEST);
echo $_REQUEST['city'];
?>
Super Global Variable $_files
$_FILES: By using this super global variable we can get the information of uploaded file. It is 2 dimensional array variable contents five elements. Each element first dimension is the name of the file upload control.
*When user upload a file from client system to server, then the file stores in server temporary location with new name. To move the file from temporary location to target location we are using “$_FILES”.
*The elements of $_FILES:
1. $_files[‘file1’][‘name’] : This element holds the file name what is given by the user.
2. $_ files[‘upload control name’][‘size’] : This variable holds the size of the uploaded file in bytes.
3. $_files[‘file upload control name’][‘type’] : This file holds he mime time of uploaded file.
4. $_files[‘name’][‘error’] : This variable holds the error number , if any accrued at the time of uploading.
5. $_files[‘name’][‘tmp_name’]: This variable holds the temporary file name of the uploaded file.
*MIME: Mime stands for multipurpose internet mailing extension. It is an execution used to transform the files from 1 location to another location using http protocols.
Ex: .jpg > img/jpeg
.pdf>application/pdf
.exe>application/octet stram.
.pdf>application/pdf
.exe>application/octet stram.
*Enc type is an attribute of a form tag used to provide the mime type of file name.
*is_uploaded_file: By using this function you can check whether the file is uploaded from client system to web server or not.
*move_uploaded_file: this function is used to move the uploaded file from temporary location to target location. It contains 2 arguments.
upload.php.
<?php
if(is_uploaded_file($_FILES['myfilecon']['tmp_name']))
{
print_r($_FILES);
$fname = $_FILES['myfilecon']['name'];
if(move_uploaded_file($_FILES['myfilecon']['tmp_name'],"d:/uploads/$fname"))
{
echo "moved";
}
else
echo "Not Moved";
}
?>
if(is_uploaded_file($_FILES['myfilecon']['tmp_name']))
{
print_r($_FILES);
$fname = $_FILES['myfilecon']['name'];
if(move_uploaded_file($_FILES['myfilecon']['tmp_name'],"d:/uploads/$fname"))
{
echo "moved";
}
else
echo "Not Moved";
}
?>
Page1.php.
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="myfilecon">
<br>
<input type="submit" value="send">
</form>
<input type="file" name="myfilecon">
<br>
<input type="submit" value="send">
</form>
hello sir, i had been using this since 2 days,got some doubts while using it,my doubt is for example i have a home page and its comprising of a login form,a article and some other contents in it,write now i want to display another article on my same home page after being logged in,i tried a lot, but not getting the soln, cud u plz help me out for this sir
ReplyDeletei tried with http:/localhost:8080 also but not working the problem is when i start the xampp control panel it says ERROR: Apache service not started [-1] even though i changed the port from 80 to 8080 sir please tell me how to know which is using port number 80 and how to stop it i went to administrative tool and saw services and could not find the service which is using port 80 .,.,., sir by the way i didnt tell u the main thin am using windows7 is it any problem please help me with it when i execute test.php it is showing as follows Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. If you think this is a server error, please contact the webmaster. Error 404 localhost 6/16/2011 3:59:25 PM Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
ReplyDelete