Wednesday, July 22, 2009
how to check data in php
{
echo "";
}
function checkdata()
{
global $error_array;
if($_POST['name']=="")
{
$error_array[]="plz entr something";
}
}
function showerror()
{
global $error_array;
foreach($error_array as $key)
{
echo $key;
}}
function handledata()
{
echo $_POST['name'];
}
$error_array=array();
if(isset($_POST["welcomeseen"]))
{
checkdata();
if(count($error_array)!=0)
{
showerror();
showwelcome();
}
else
{
handledata();
}
}
else
{
showwelcome();
}
Monday, July 20, 2009
date difference
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
$date1="07/11/2003";
$date2="09/04/2004";
$dob="08/12/1975";
echo "If you were born on " . $dob . ", then today your age is approximately " . round(dateDiff("/", date("m/d/Y", time()), $dob)/365, 0) . " years.";
?>
how to enter phto in mysql using php
input name="userfile" type="file" id="userfile
input name="upload" type="submit" class="box" id="upload" value=" Upload
if(!$con)
{
die("conection failed".mysql_error());
}
$sql=mysql_select_db("photo",$con);if(!$sql){die("database failed".mysql_error());}
$uploadDir = 'C:/webroot/upload/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result)
{
echo "Error uploading file";exit;}
if(!get_magic_quotes_gpc())
{$fileName = addslashes($fileName);
$filePath = addslashes($filePath);}
$query = "INSERT INTO upload2 (name, size, type, path ) "."VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
mysql_close($con);
echo "
Files uploaded
";
}?>
Saturday, July 11, 2009
php6-control structure
divided into two groups: conditional control structures and loop control structures. The conditional control structures affect the flow of the program and execute or skip certain code according to certain criteria, whereas loop control structures execute certain code an arbitrary number of times according to specified criteria
A.Conditional Control Structures
Conditional control structures are crucial in allowing your program to take different execution paths based on decisions it makes at runtime. PHP supports both the if and switch conditional control structures.
1.if statement
The PHP if statement tests to see if a value is true, and if it is a segment of code will be executed.
$my_name = "someguy";
if ( $my_name == "someguy" )
{
statement1;//the coding appear here
}
?>
2.if..else
if if part is correct than statement1 execute else statement2 will execute.
$number_three = 3;
if ( $number_three == 3 ) {
statement1;
}
else
{
statement2;
}
?>
3.if...elseif
When PHP evaluates your If...elseif...else statement it will first see if the If statement is true. If that tests comes out false it will then check the first elseif statement. If that is false it will either check the next elseif statement, or if there are no more elseif statements, it will evaluate the else segment, if one exists
$employee = "Bob";
if($employee == "Ms. Tanner")
{
echo "Hello Ma'am";
}
elseif($employee == "Bob")
{
echo "Good Morning Sir!";
}
else
{
echo "Morning";
}
?>
4.switch
The way the Switch statement works is it takes a single variable as input and then checks it against all the different cases you set up for that switch statement. Instead of having to check that variable one at a time, as it goes through a bunch of If Statements, the Switch statement only has to check one time.
$destination = "New York";
echo "Traveling to $destination
";
switch ($destination){
case "Las Vegas":
echo "Bring an extra $500";
break;
case "Amsterdam":
echo "Bring an open mind";
break;
case "Egypt":
echo "Bring 15 bottles of SPF 50 Sunscreen";
break;
case "Tokyo":
echo "Bring lots of money";
break;
case "Caribbean Islands":
echo "Bring a swimsuit";
break;
default:
echo "Bring lots of underwear!";
break;
}
?>
php5-operator
+ Addition 2 + 4=6
- Subtraction 6 - 2=4
* Multiplication 5 * 3=15
/ Division 15 / 3 =5
% Modulus 43 % 10 =3(show the remainder)
1.Assignment Operators
operators:-
+=, -=, *=, /=, %=, ^=, .=, &=, |=, <<=, >>=
For example:
$counter += 2; // This is identical to $counter = $counter + 2;
$offset *= $counter;// This is identical to $offset = $offset * $counter;
2.Comparison Operators
Comparison operators enable you to determine the relationship between two operands
== Equal to
Checks for equality between two arguments performing type conversion when necessary:
1 == "1" results in true
1 == 1 results in true
!= Not equal to
Inverse of ==.
> Greater than
Checks if first operand is greater than second
< Smaller than
Checks if first operand is smaller than second
>= Greater than or equal to
Checks if first operand is greater or equal to second
<= Smaller than or equal to
Checks if first operand is smaller or equal to second
3.Logical Operators
Logical operators first convert their operands to boolean values and then perform the respective comparison.
Operator
&&, and
The result of the logical AND operation between the two operands
||, or
if the condition with of any operand is true than result is true
ex-$a=45;
if(a>30||a<50)
echo "arunrawat-web.blogspot.com";
//this print the arunrawat-web.blogspot.com
?>
Xor, Logical XOR
The result of the logical XOR operation between the two operands
increment/decrement operator
$var++, Post-increment
$var is incremented by 1.
The previous value of $var.
++$var, Pre-increment
$var is incremented by 1.
The new value of $var (incremented by 1).
$var--
Post-decrement
$var is decremented by 1.
The previous value of $var.
--$var, Pre-decrement
$var is decremented by 1.
The new value of $var (decremented by 1).
php4-managing varibles
Three language constructs are used to manage variables. They enable you to check if certain variables exist, remove variables, and check variables' truth values.
1.isset()
isset() determines whether a certain variable has already been declared by PHP. It returns a boolean value true if the variable has already been set, and false otherwise, or if the variable is set to the value NULL. Consider the following script:
if (isset($first_name)) {
print '$first_name is set';
}
This code snippet checks whether the variable $first_name is defined. If $first_name is defined, isset() returns true, which will display '$first_name is set.' If it isn't, no output is generated.
isset() can also be used on array elements (discussed in a later section) and object properties. Here are examples for the relevant syntax, which you can refer to later:
Checking an array element:
if (isset($arr["offset"])) {
...
}
Checking an object property:
if (isset($obj->property)) {
...
}
Note that in both examples, we didn't check if $arr or $obj are set (before we checked the offset or property, respectively). The isset() construct returns false automatically if they are not set.
isset() is the only one of the three language constructs that accepts an arbitrary amount of parameters. Its accurate prototype is as follows:
isset($var1, $var2, $var3, ...);
It only returns TRue if all the variables have been defined; otherwise, it returns false. This is useful when you want to check if the required input variables for your script have really been sent by the client, saving you a series of single isset() checks.
2. unset()
unset() "undeclares" a previously set variable, and frees any memory that was used by it if no other variable references its value. A call to isset() on a variable that has been unset() returns false.
For example:
$name = "John Doe";
unset($name);
if (isset($name)) {
print '$name is set';
}
This example will not generate any output, because isset() returns false.
unset() can also be used on array elements and object properties similar to isset().
3.empty()
empty() may be used to check if a variable has not been declared or its value is false. This language construct is usually used to check if a form variable has not been sent or does not contain data. When checking a variable's truth value, its value is first converted to a Boolean according to the rules in the following section, and then it is checked for TRue/false.
For example:
if (empty($name)) {
print 'Error: Forgot to specify a value for $name';
}
This code prints an error message if $name doesn't contain a value that evaluates to true
php3-variables
it is very easy to declare variables in php because there is no need to declare their type like in java or c.
$ sign is used in the front of the variable .
$a is the variable
There are a few rules that you need to follow when choosing a name for your PHP variables.
•PHP variables must start with a letter or underscore "_".
•PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
•Variables with more than one word should be separated with underscores. $my_variable
•Variables with more than one word can also be distinguished with capitalization. $myVariable
Indirect References to Variables
An extremely useful feature of PHP is that you can access variables by using indirect references, or to put it simply, you can create and access variables by name at runtime.
Consider the following example:
$name = "John";
$$name = "Registered user";
print $John;
This code results in the printing of "Registered user."
The bold line uses an additional $ to access the variable with name specified by the value of $name ("John") and changing its value to "Registered user". Therefore, a variable called $John is created.
You can use as many levels of indirections as you want by adding additional $ signs in front of a variable
Superglobals
As a general rule, PHP does not support global variables (variables that can automatically be accessed from any scope). However, certain special internal variables behave like global variables similar to other languages. These variables are called superglobals and are predefined by PHP for you to use. Some examples of these superglobals are
$_GET[]. An array that includes all the GET variables that PHP received from the client browser.
$_POST[]. An array that includes all the POST variables that PHP received from the client browser.
$_COOKIE[]. An array that includes all the cookies that PHP received from the client browser.
$_SERVER[]. An array with the values of the web-server variables.
php2-comments
comment r very necessay for the programmer to made for future use.A good programmer always declare the cooments
// is used for single line cooment
/*
this is used for multiple line comment
*/