Sunday, December 23, 2012

php more 2

PHP is probably the most popular scripting language on the web.
PHP is known as a server-sided language. "Hypertext Pre-processor".
You need wamp  server
Saving your PHP files
Whenever you create a new PHP page, you need to save it in your WWW directory.
variable
A variable is just a storage area. You put things into your storage areas (variables) so that you can use and manipulate them in your programmes. Things you'll want to store are numbers and text.
Variables cant start with number or underscore
Variable must start with $ sign and end with semi colon ;
If you miss the dollar sign out, then your people will refuse to work! But the other thing missing is something really picky and fussy - a semi-colon. Lines of code in PHP need a semi-colon at the end:
$mr_coats = 10;
If you get any parse errors when you try to run your code

Putting Text into Variables


Now how to add text and not numbers into variables
$coats=”winter”;
You need to put text into double or single quotes and rest is sam e
Some practice on variables
$any="variable";
print ($any);
?>
Yaad rakho jo “” ke andar hogi vo print hogi….jo nahi hogi uski value php upper code mein dhundega


Print(“worked”);
?>
Both are worked

$first_number = 10;
print ($first_number);
?>

To print number don’t need quotes
Now question arise how to print this :- my roll no is 50
And answer is with the use of concatenate (dot.)
$first_number = 10;
$direct_text = 'My variable contains the value of ';
print ($direct_text . $first_number);
?>
How to add two numbers and print with text as shown
$first_number = 10;
$second_number = 20;
$sum_total = $first_number + $second_number;
$direct_text = 'The two variables added together = ';
print ($direct_text . $sum_total);
?>

Multiplication ke liye – sign use karlo and multiplication ke liye * sign ok
And for division / sign
$first_number = 1.2;
$second_number = 2.5;
$sum_total = $second_number + $first_number;
print ($sum_total);
?>

The above is to add floating point numbers


Copy the images folder to your www (root) directory. Then try the following script:
print ("");
?>


In PHP, you use the "IF" word like this:
if ($User_Name = = "authentic") {
//Code to let user access the site here;
}
Without any checking, the if statement looks like this:
if ( ) {
}

We can use an if statement to display our image, from the previous section. If the user selected "church", then display the church image. If the user selected "kitten", then display anoth image (the kitten image, which is also in your images folder). Here's some code:
$kitten_image = 1;
$church_image = 0;
if ($kitten_image == 1) {
print ("");
}
?>

Remember image bhi double quotes mein ati hai
Similar coding for if-else
$kitten_image = 0;
$church_image = 1;
if ($kitten_image == 1) {
print ("");
}
else {
print ("");
}
?>

If-else-if
$kitten_image = 1;
$church_image = 0;
if ($kitten_image == 1) {
print ("");
}
else if ($church_image == 1){
print ("");
}
else {
print ("No value of 1 detected");
}
?>

Comparison operayor != not equal to example
$correct_username = 'logmein';
$what_visitor_typed = 'logMEin';
if ($what_visitor_typed != $correct_username) {
print("You're not a valid user of this site!");
}
?>

$correct_username = 'logmein';
$what_visitor_typed = 'logmein';
if ($what_visitor_typed != $correct_username) {
print("You're not a valid user of this site!");
}
else {
print("Welcome back, friend!");
}

less than greator than
$total_spent = 110;
$discount_total = 100;

if ($total_spent > $discount_total) {
print("10 percent discount applies to this order!");
}
?>

$total_spent = 90;
$discount_total = 100;
if ($total_spent > $discount_total) {
print("10 percent discount applies to this order!");
}
else if($total_spent < $discount_total) {
print("Sorry – No discount!");
}
?>

Switch statement is a little bit complex to understand but easy …..
$picture ='church';
switch ($picture) {
case 'kitten':
print('Kitten Picture');
break;
case 'church':
print('Church Picture');
break;
}
?>
In this hamne variable picture ki value di hai church….now we test switch($picture)….its a syntax u ned to learn….
Ab dekho case mein kitten to print ho jaye kitten picture but value to hamne church di thi variable ki to case 2 print ho jayega….
It’s a easy way to test…as compared to if else if …


The && Operator
The && symbols mean AND. Use this if you need both values to be true, as in our username and password test. After all, you don't want to let people in if they just get the username right but not the password! Here's an example:
$username ='user';
$password ='password';
if ($username =='user' && $password =='password') {
print("Welcome back!");
}
else {
print("Invalid Login Detected");
}

The | | Operator
The two straight lines mean OR. Use this symbol when you only need one of your conditions to be true. For example, suppose you want to grant a discount to people if they have spent more than 100 pounds OR they have a special key. Else they don't get any discount. You'd then code like this:
$total_spent =100;
$special_key ='SK12345';
if ($total_spent ==100 | | $special_key =='SK12345') {
print("Discount Granted!");
}
else {
print("No discount for you!");
}


Boolean
$true_value = true;
$false_value = false;
print ("true_value = " . $true_value);
print (" false_value = " . $false_value);
?>

Note and true and false used without quotes… and it will give result
True value=1 and false value=…. Nothing
Now replace true with 1 and flase with 0 …now it works
Above can be used somewhere in codings also

$true_value = true;
if (!$true_value) {
print("that's true");
}
else {
print("that's not true");
}

get v/s post method

getting value:-
NAME = "username"
>
2nd page
$username = $_POST['username'];
print ($username);
?>

Or
$_POST['formElement_name'];  (without variables)

So how does it work?
The $_POST[] is an inbuilt function you can use to get POST data from a form. If you had METHOD = "GET" on your form, then you'd used this instead:
$username = $_GET['username'];

Uses in coding :-
$username = $_POST['username'];
if ($username = = "letmein") {
print ("Welcome back, friend!");
}
else {
print ("You're not a member of this site");
}

1st save as


A BASIC HTML FORM

Action ="submitForm.php
">
Name ="username">



Now create the following page, and call it submitForm.php.
$username = $_POST['username'];
if ($username = = "letmein") {
print ("Welcome back, friend!");
}
else {
print ("You're not a member of this site");
}
?>

No comments:

Post a Comment