Showing posts with label value. Show all posts
Showing posts with label value. Show all posts

Saturday, 5 April 2014

Calculator In PHP

Calculator in php Script
Calculator In Php

Php Code For making calculator In Php Which will perform functions such as addition,subtraction,Multiplication,Division.






Code For Calculator In Php


<?php

//The following is the calculator code:



ini_set('display_errors',0); //ini_set for error reporting

if( isset( $_REQUEST['calculate'] ))//isset check whether value is null or not.

{

$operator=$_REQUEST['operator'];

if($operator=="+")// add operator

{

$add1 = $_REQUEST['fvalue']; // getting values of html form

$add2 = $_REQUEST['lvalue']; // getting values of html form

$res= $add1+$add2;//adding values

}

if($operator=="-")//substracting operator

{

$add1 = $_REQUEST['fvalue'];// getting values of html form

$add2 = $_REQUEST['lvalue'];// getting values of html form

$res= $add1-$add2;//substracting values

}

if($operator=="*")//multipilcation operator

{

$add1 = $_REQUEST['fvalue'];//  getting values of html form

$add2 = $_REQUEST['lvalue'];//  getting values of html form

$res =$add1*$add2;//multiply values

}

if($operator=="/")//divide opeartor

{

$add1 = $_REQUEST['fvalue']; // getting values of html form

$add2 = $_REQUEST['lvalue']; //  getting values of html form

$res= $add1/$add2;divide values

}



}

?>

<form> // html form

Enter First Number<input name="fvalue" type="text">

            Select Operator <select name="operator">

<option>+</option>

<option>-</option>

<option>*</option>

<option>/</option>

</select>

Enter Second Number<input name="lvalue" type="text"/>

<input type="submit" name="calculate" value="Calculate" />

                Output =

               <?php echo $res;?>// output results

 </form>


Output Screenshot1

Calculator In Php
Screenshot1
In this example i had made calculator to calculate two values for addition, subtraction, Multiplication, Division.

Refer Articles :-
Articles For Detail Study Programming In Php. Details About Php Code Which I used to make Calculator In php. 
Read More

Friday, 21 March 2014

Working with Form data , $_GET , $_POST Variables Html Forms in Php

Form data variables in php
Working with Form data ,  $_GET , $_POST Variables Html Forms back to basic how we can use it in different elements of in that allow as to process data through php programming language. To sent the data to page In this example i am using same page for posting. You can post same page where you are on currently or at another page. Now let see each elements of html form. Name is something to name the textbox that we can pick up for using and processing user input.



Code for working with Form Data 


<form action = "index.php" method="GET">

Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output

Working with Form data ,  $_GET , $_POST Variables Html Forms in Php
Screenshot1
<from> tag is starting of the form and </from> tag is ending of the form. We also can specify action a method now action is where this form is submit when the submit button is clicked. So, we include submit button in this inside form. Here i want my page to link back to same page to index.php you can also redirect to another page. The method can be GET or POST. Depending upon submitting variables. 

$_GET variable is sending data visible in url sending variable through format of url.

$_POST variable is sending data its send data through http post method.

So, Here i am using GET method then take some input method of text type to display date, time and year. take this value from the user.


Working with $_Get Variables in Php programming language. Html forms we may know html forms are used for getting user input get method is useful for forms. 

Code for Working With $_GET Variables (Screenshot1):-

working with global variables
Screenshot1


<?php



if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

$day = $_GET['day'];

$date = $_GET['date'];

$year = $_GET['year'];

if(!empty($day)&&!empty($date)&&!empty($year)) {

echo 'It is '.$day.' '.$date.''.$year;

}else {

echo 'Fill in all fields.';

}

}

?>



<form action = "index.php" method="GET">



Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output (Screenshot2):-


working with form data, $_GET, $_POST variables
Screenshot2
creating html form explain the action method variable GET & POST. $_GET and $_POST variable of html form we can use it in php programming language. For picking up forms variable to php variable. In this example we are processing three variable in get method date, date and year. From the help of html forms we need value to pick up and processes to php and display to the user in this example.

Now in php what we need to do is firstly check few conditions 

1. Does It exist? or "Has it been submitted?"

example code
if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

It will check whether user had submitted the form that will check submit value of submit button.

2. Is it empty? or "Does value == NULL"

example code
if(!empty($day)&&!empty($date)&&!empty($year))

It will check whether submitted value is empty or null value.

3. Display back to user

example code

echo 'It is '.$day.' '.$date.''.$year;
}else {
echo 'Fill in all fields.';
}
}

If both condition will true it will output the user what the user inputted in the form values. 

Now you will be thinking how does the values of form detected in php variable 

example code
$day = $_GET['day'];
$date = $_GET['date'];
$year = $_GET['year'];

In this way we can save form values into the php variables. $_GET method is useful to grab the values from the form to the php variables the name of the textbox field can be use to grab the values in $_GET method.

Day:<br><input type="text" name="day"> -> $day = $_GET['day'];.

The attribute name of the html form is used into the $_GET method and then it can be declare had php variables.
$_POST Variables in php
Screenshot3


Working with $_POST Variables 


Post data using forms now we already seen get data using forms using post data is because for security. In $_GET variable we were displaying value of from into the url. We were showing value name of the form and its value in our url in $_GET method of the form. You can see the structure of $_GET method variables in Screenshot2 and Screenshot3. But in $_POST variable the url of the page will be the file name not the values or anything will show to the url its the only difference between $_GET and $_POST variable. The $_POST variable is used in long text area like description of anything , in registration form for password because password must not be pass to the url to show user and url had limitation some amount of text we can show in url therefore for the long description we can not use $_GET variable.

Code for With $_POST Variables :-


<?php



$match = 'pass123';



if(isset($_POST['password'])) {

$password = $_POST['password'];

if(!empty($password)) {

if($password==$match){

echo 'That is correct!';

}else{

echo 'That is incorrect!';

}

}else{

echo 'Please enter a password.';

}

}



?>



<form action="index.php" method="POST">

password:<br>

<input type="password" name="password"><br><br>

<input type="submit" value="submit">

</form>

Output (Screenshot4)

variables in php
Screenshot4
variables in html forms
Screenshot5

In this example i am checking whether user password is matched with the static variable $match. You can see how $_POST variable is used same as $_GET Variable just by replacing word GET to POST. All things are the same here but the major difference is of url in $_GET method url can pass form values as well as $_POST variable just include file name thats it.

Now as you compare screenshot5 and Screenshot screenshot2 url you will come to the whats its happening. 

Read More

Saturday, 1 March 2014

Functions With a Return Value in php

In this tutorial we are going to deal with function with a return value.We have already seen two other tutorials of basic function and function with arguments. Generally we echo out the content of function inside it only,but if we want to echo out the content outside the function, we will use “return” keyword. Here, “return” keyword returns a specific value the value which can be calculated inside function that can be a string or integer value. Hence the value which is returned outside the function and is stored in any variable of the same datatype as that of variable that is being returned, can be echoed out easily. Example:- If we want to divide two number before adding it as:- (10, 10)/ (5, 5) = 2 Here,10 should get added with 10 and 5 should get added with 5 & then divide 20 / 10 which will give 2 as answer (Screenshot1).

Function WIth a return value in php
Screenshot1

Code for function with a return value (Screenshot2):-

Code for Return value in function
Screenshot2



<?php



function add($number1, $number2) {

$result = $number1 + $number2;

return $result;

}



add(10, 10);



?>

Output (Screenshot3):-

return value
Screenshot3
In this example, I have created the function for adding two number and storing value to “result” variable and then return the value from functions. Here, it will not echo out anything in browser because its just storing value to function by return keyword.

Code for function with return value (Screenshot4):-

Return value in functions
Screenshot4

<?php



function add($number1, $number2) {

$result = $number1 + $number2;

return $result;

}



echo add(10, 10);



?>

Output (Screenshot5):-

Function In Php
Screenshot5
In this example, I have created the function for adding two number and storing value to “result” variable and then return the value from functions. Here, it will echo out values via variable “sum” by calling functions.

Code for functions with return value adding and divide it(Screenshot6):-

Function return value
Screenshot6

<?php



function add($number1, $number){

$result = $number1 + $number2;

return $result;

}



function divide($number1, $number2) {

$result = $number1/ $number2;

return $result;

}



$sum = divide(add(10, 10), add(5, 5));

echo sum;



?>

Output (Screenshot7):-

php functions
Screenshot7
Here,firstly we have to make two functions to add two variables and then divide them. As we seen in screenshot4 we are adding two number by calling function, passing values to variables, storing values of the result and then retrieving value of result outside the function. Similarly, for divide function too.It will work as in first (Screenshot1) (10 + 10) / (5 + 5) = 2.
In this way you can use function many time by calling and passing variables; every time functions variables will be changed which is every useful for us to reuse the functions. Its easy for the programmer as it shorts the codes in the program.
Read More

Thursday, 13 February 2014

Variables in php

php variables
Variables
This tutorial is for variables. If you have worked with any other programming language you know what are variables for and what they will do.Values are assigned to variables and variables are of different types.Now in this tutorials i am going to show you how to declare variables by assigning values to standard structure and then variables are echoed out on the browser.So, Usually in programming languages such as JavaScript or for any other language like it we have to use keywords,for example in java we use “var” like var example.





<?php

var name = 'value';

?>



In php we don’t need any keywords to define anything only we need to assign it with “$” dollar sign notation and afterwards we can assign “_” sign or characters or strings or numeric characters also. Assigning can be done in upper-case and lower-case both.
Code for Variables(Screenshot1) :-

Variables
Code For Variables Screenshot1


<?php

echo $text = 'Hello World.';

echo $number = 100;

?>



output (Screenshot2) :-

Code for Variables
Screenshot2


In Screenshot1 you can see codes for string.I have used sign ” notation for numeric value and i am not using any notation but only semi colon “;” . Semi colon “;” is a separator. Echo statementecho’s out the variables.We can assign any value to variables such as floating values, Boolean values,etc., but this we will see later on in if statements.

Code for variables different way (Screenshot3) :-

Variables in php
Code for Different types for variables Screenshot3


<?php

$text = 'Hello World.';

$number = 100;

 echo $text;

?>


Output (Screenshot4) :-

code for variables in php
Variables Screenshot4

It will only echo variable $text contain because we only echoing $text variable Code for variables different way second example (Screenshot5):-

Screenshot5


<?php

$text = 'Hello world.";

$number = 100;

echo $number;

?>

Output(Screenshot6) :-

Variables string
Screenshot6


It will echo out the content of variable $number because we are echoing variable $number. Programming languages are used todeclare different data’s, for example you want to take users age or take users name or anything you need to declare,so either you will store it as string data or you will store it as numerical data like integers (similarly for floats),which is somewhat tedious ! But, I would like to tell you an important aspect of php , that you don’t need to think about what data you are storing, you just simply need to assign “$” Sign in the prefix of variable name following with equal to and the variable value. Declaration of variables in php is quite easy.

Read More

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates