Tuesday, 10 May 2011

Learn PHP online:PHP Day-9

Variables In PHP



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()
?>
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;
?>
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();
?>
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;
?>



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>
Var.php
<?php
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.
@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>
Page1.php
<?php
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.


*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";
}
?>
Page1.php.

<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="myfilecon">
<br>
<input type="submit" value="send">
</form>

No comments:

Post a Comment

Visual comparison of the two methods, creating a simple table.

Option 1, using PHP: // PHP $html = '<table>' ;     foreach ( $data as $row ) {     $html .= '<tr>...