Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

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, 27 February 2014

Switch Statement in php

php control structures
Switch Statement in PHP Programming Languages. This tutorial is for Switch Statement. Now you may already observed in my earlier tutorials of if_else statement and if_else if statement for multiple checks, but what if you have a lot of numbers or strings to check or something you need to check like long list. Switch Statement is a better option . Its better, cleaner and faster way of using if _else and else if statements.

Code for switch statement in php (Screenshot1):-

Code For Switch Statement In php
Code For Switch Statement In php Screenshot1

<?php

$number = 1;

switch ($number) {

 case 1:

 echo 'One';

 break;

case 2:

 echo 'Two';

 break;

case 3:

 echo 'Three';

 break;

}

?>



Output (Screenshot2):-

Switch Statement
Screenshot2
In Switch Statement structure we have “switch” as a keyword where we refer declared value of variables in switch. Inside the switch blog we can evaluate multiple operations in different cases. Inside case’s along with operations we can call/declare functions, echo any statement ,and the very important thing to notice is to write “break” keyword at the time of ending any case. Eg. in below example we have a variable $number that is initialized as 2 and in switch keyword we have given reference to that variable to evaluate it further with case.In this program we had convert integer data to string. So, instead of writing number equal to 1 echo “one”, else if number equal to 2 echo “two”, else if number equal to 3 echo “three”, but this will confuse programmer in large programs if used!Switch Statement makes it easy by taking
case 1: echo one
case 2: echo two
case 3 echo three
and basically doing this evaluates our number too.

Code for Switch Statement in php (Screenshot3):-

Switch case
Code For Switch Statement in php Screenshot3

<?php

$number = 2;

switch ($number) {

 case 1:

 echo 'One';

 break;

case 2:

 echo 'Two';

 break;

case 3:

 echo 'Three';

 break;

}

?>

Output (Screenshot4):-

Switch statement in php
Screenshot4
In this example variable is 2 so its true for case 2 which will echo “two”.

Code for Switch Statement in php (Screenshot5):-


switch statement php
Screenshot5


<?php

$number = 2;

switch ($number) {

 case 1:

 echo 'One';

 break;

case 2:

 echo 'Two';

 break;

case 3:

 echo 'Three';

 break;

}

?>

Output (Screenshot6):-

php switch case
Screenshot6
In this example variable is 3 so its true for case 3 which will echo “three”.

Code for Switch Statement in php (Screenshot7):-

Code for switch statement
Screenshot7

<?php

$number = 4;

switch ($number) {

 case 1:

 echo 'One';

 break;

case 2:

 echo 'Two';

 break;

case 3:

 echo 'Three';

 break;

default:

 echo 'Number not found';

 break;

}

?>

Output (Screenshot8):-

Switch case php
Screenshot8
In this example what if number does not evaluate any case, for handling this we have default case. In this example case 4 does not exit. So,via default case we will echo out “Number not Found”.

Code for switch statement (Screenshot9):-

switch case php
Screenshot9

<?php

$day = 'Monday';
switch ($day) {

 case 'Saturday':

 case 'Sunday':

 echo 'It\'s a weekend.';

 break;

default:

 echo 'Not a weekend.';

 break;

}

?>

Output (Screenshot10):-

switch case php
Screenshot10
In this example I am taking variable string and evaluate it with string to echo out whether it is “weekend” or “Not a weekend”. For Monday it’s “Not a weekend”.

Code for switch statement (Screensho11):-

switch case
Code for switch case Screenshot11


<?php

$day = 'Saturday';
switch ($day) {

 case 'Saturday':

 case 'Sunday':

 echo 'It\'s a weekend.';

 break;

default:

 echo 'Not a weekend.';

 break;

}

?>

Output (Screenshot12):-

Output Screenshot12
In this example I am taking variable string and evaluate it with string to echo out whether it is “weekend” or “Not a weekend”. For Saturday ”It’s a weekend”.
You can try for Sunday as well by changing variable to Sunday and thus it evaluates case for Sunday. So,
in thus in Switch case you can compare multiple cases and always use default for handling variables not evaluating or comparing cases.
Read More

Sunday, 16 February 2014

Concatenation in php

concatenation
"Con","cat","en","ation"
This is tutorial for concatenation within php. Now if you ever come across working with concatenation you will come to know about it in a better way.Before programming you may understand how to concatenate different variables together.Later on, How to printvariables which are concatenated in,Its very very simple in php.

Code for Concatenate in php (Screenshot1):-

Concatenation in php
Concatenation in php Screenshot1
 <?php

$day = 'Thursday';

$date = 31;

$year = 2011;



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

?>

Output (Screenshot2) :- 

Concatenation in php
Screenshot2
 In this code of  concatenate in php I am creating three variables i.e. $day, $date, $year.Now to echo one or more variables we have to join variables with dot which is called as concatenate. In single quotation (‘) inside echo statement we are echoing variables and text.Blank single quotation (‘) is used to leave space between variables.With Single quotation (‘) concatenate feature is most useful and with Double quotation (“) too its easy which I will so you in later tutorials.

Code For Concatenate in php (Screenshot3):-

Screenshot3

<?php

$day = 'Wednesday';

$date = 30;

$year = 2011;



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

?>

Output (Screenshot4) :-




In this examples i just updated variables and its automatically update output.So this is harder but fastest way to echo out variables by concatenate in string data.

Code for concatenate in php (Screenshot5):-
Concatenation in php
 Screenshot4


In this examples i just updated variables and its automatically update output.So this is harder but fastest way to echo out variables by concatenate in string data.

Code for concatenate in php (Screenshot5):-

 
concatenation in php
Screenshot5

<?php

$day = 'Wednesday';

$date = 30;

$year = 2012;



echo 'The date is <strong>'.$day.' '.$date.' ' .$year.'</strong>;

?>


Output (Screenshot6):-

concatenation in php
Screenshot6

In this example i am including html tags strong tag with concatenate strings, variables and also html tags.In this code it will bold variables.

Code for Concatenate in php with Double quotation marks(Screenshot7):-

Concatenation in php
Screenshot7

<?php

$day = 'Wednesday';

$date = 30;

$year = 2011;



echo "The date is $day $date $year";

?>



Output (Screenshot4) :-

Concatenation in php
Screenshot4

In this example we used Double quotation marks to concatenate string and variables and its easy because here you don’t write any dot (.) or to leave space in single quotation marks. Its very easy to Concatenate with double quotation marks.But single quotation marks are faster.Concatenate looks complex code in understanding there so i am recommending to always use single codes.

Code for Concatenate in php (Screenshot8):-

Concatenation in php
Screenshot8

<?php

$day = 'Wednesday';

$date = 30;

$year = 2011;



echo 'The date is $day $date $year';

?>

output (Screenshot9):-

Concatenation in php
Screenshot9
In this code if we use single quotation mark without concatenate with dot (.) it will echo variable name and does not reference to variable values.
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

Sunday, 9 February 2014

Installing Xampp Part -1

This is Php Tutorial of Tutorials JackPot. If you want to start as php developer, Then firstly you need to have a platform to test your codes. You must have already purchased your paid hosting check whether they allow php extensions or not ! 99% paid or free hosting allows you to run php on the server. Its pretty much obvious that in most cases most of the hosting companies gives this service to its customer.


Hosting
Hosting

What happens when you want to test everything locally on your computer.So firstly, create a php file and upload it to the server as above ,but we want to quickly and easily obtain its result on the local machine. So what we can do is that we can turn over our computer into a web server. Now, its highly not recommended to allow access to the web server and the files if its not properly configured, Otherwise you can have some security issues there. Let me show you a screenshot of how you can access an another portion your computer. Opening a port 80 on your computer which allow people to access everything you save into your htdoc folder. As going ahead, Ill explain you about how to create or install a mini web server on to your computer. As I am personally using “Xampp” I would advice you to download and install “Apache” as a web-server for php. Apache web-server allows you to compile in the background server, Since php is a server side language.It allows you to compile php files, run them and display the content of your programs in the browsers.

Localhost
LocalHost
So, basically you need something like xampp or wamp. I am showing you wamp so its available however i am going to be dealing with xampp and xampp by default directory things like that. So, if you are following this tutorial i am using xampp or otherwise you can take wamp and see you can get idea from this tutorial.

Xampp
Xampp
However you can also use “Wamp” for php which works on “IIS” server that can be easily installed on “Windows OS”.Working of both Xampp and Wamp are almost same,just the difference is about the folder where the file is saved.


Redirecting to Xampp, Firstly friends you need to visit Appache Friends.There you will find Xampp for Linux, Xampp for Windows, Xampp for Mac os x, Xampp for Solaris. Php can easily work on any of this operating system and its also free of cost.For support on how to install Xampp here are some screenshots.Download Xampp by clicking it on Xampp s Xampp Installation page

or
Method B : “Installation” without the installer   ( I highly recommend you to go with the installer method A.)




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