Friday, 28 February 2014

Functions with Arguments in php

call by reference
Functions With Arguments in Php Programming Languages. This tutorial is about functions passing arguments to functions.Functions are useful in code to shorten the amount of code to write and you can refer back this functions too which is known as calling of functions and it will process the same code every-time you call.

Code basic Structure of functions (Screenshot1):-

Functions With Arguments In php
Screenshot1

<?php



function name() {

   /// here

}



name();



?>


This is basic and standard structure of functions.

Code for adding numbers by functions (Screenshot2):-

Code for adding numbers by Functions Screenshot2

<?php
function add($number1, $number2) {
echo $number1 + $number2;
}
add();


?>

In this example,I have created a function adding two numbers.You may do programming as it is simply done to add two numbers. You may take user input as number1 and number2 and for that you may need functions to add to numbers together. Always get practiced of using functions instead of structures as its lot better then doing things on the fly. By calling functions specially with arguments you save a lot of time,better and as well as easier to read.
In this example, I am making function add and inside the function specify those arguments which are in the form of variables.Here my variables are $number1 and $number2. Here the variables type are unspecified that whether they are numbers or string which actually doesn't matter.That depends on how you deals everything inside functions.Now echo out that particular results where $number1 will refer to functions variable. The functional arguments must match that is used in the functions. Here let me show you calling functions without arguments to show you errors, and how to read errors.
In output Screenshot3
Now you can see in output warning coming up which is missing argument 1 for add(). Its telling us first argument is missing for function add, it tells us in which line code is and it also tell us where the functions has been declared. And also its missing argument 2 for add() function and code in same line and function is defined in same line and actually it gives the results.

Code for adding two numbers in functions with arguments(Screenshot4):-

Code for adding two numbers in functions with arguments Screenshot4
<?php



$inumber1 = 10;

$inumber2 = 5;



function add($number1, $number2) {

echo $number1 + $number2;

}



add($inumber1,$inumber2);

?>

Output (Screenshot5):-

Functions with arguments in php
Screenshot5
In this example of adding two number we are including some numbers. Taking two variables like inumber1 and inumber2. Now as we created variables $number1= 10 and $number2= 5 so, result is 15.Now when we call function we need to specify that two variables ,this two variables will pass as $inumber1 will be pass through function variable $number1 and $inumber2 will be pass through function variable $number2. That the only arguments we need to pass through functions.Now calling in add() that variables will be interpreted, then inside functions they are processed.then we echo out the results.And you can see results 15.

Code for adding two numbers in functions with arguments(Screenshot6):-

Screenshot6
<?php



$inumber1 = 22;

$inumber2 = 8;



function add($number1, $number2) {

echo $number1 + $number2;

}



add($inumber1,$inumber2);

?>

Output (Screenshot7):-

functions with arguments in php
Screenshot7
In this examples codes are same just changed variables to different integer.

Code for functions with arguments to display date (Screenshot8):-

functions php
Screenshot8
<?php



function displayDate($day, $date, $year) {

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

}



displayDate('Monday',31,2011);



?>


Output (Screenshot9):-

arguments in php functions
Screenshot10


In this example,I am making functions to print date.Making functions of displaying date will feed in the day , the date and the year.Then echo out the result into the functions.Here our day will be string then date will be integer and year will be integer.Now on calling the function displayDate(), passing variables string as well as integer will be displayed.In this way arguments can be pass in php.
Read More

Basic Functions in php

Functions in php
Basic Functions in PHP Programming Languages. This tutorial is for Basic Functions. Why functions are useful in php and also in other programming languages? Lets see how we can create functions and use a function with our page. Firstly let me know you a function is something which allows you to declare a block of code that we may want to reuse,we use more arguments which is sometimes refractive.

Code for basic structure for functions(Screenshot1):-

Basic Functions In Php
Code for Basic Structure for Functions In Php Screenshot1

<?php



function MyName() {

echo 'Mohammed';

}



echo 'My name is <br>';

MyName();

?>

Output (Screenshot2):-

Basic function in php
Screenshot2
In this examples of basic structure of functions there is keyword “function” along with name of function i.e “MyName” and conditions in curly brackets. Since this example is basic so I am just showing you to echo out my name using functions. Then when we want to use functions we just had to call the function by name and conditions inside curly brackets are applied.
Now you need to think about it logically how you can use functions in your programs.
Read More

Die and Exit Functions in Php

die and exit function in php
Die and Exit Functions in PHP Programming Languages. This tutorial is for Die & Exit Functions. They are very useful functions that you should get to use into your program so you can cover the script at any point where you made fatal error and which you don’t want to execute . Let see how it works:-

Code for die function (Screenshot1):-

die function php
Screenshot1
<?php

echo 'Hello';

die();

 echo ' World';

?>



Output Screenshot2
Die Function php
Screenshot2
In this example I had echo out “Hello” and “World”. But in output its just echoing out me “Hello” because of using Die Function. I have kill the code after die()
keyword with brackets,when you write the function from that point it will kill rest of the page.

Code for exit function(Screenshot3):-

Screenshot3
<?php

echo 'Hello';

exit();

 echo ' World';

?>

Output (Screenshot4):-

die exit function php
Screenshot4
In this example doing same thing using exit() with brackets.

Code for die or exit function(Screenshot5):-

die or exit functions php
Screenshot5

<?php

echo 'Hello';

die('ERROR. PAGE HAS ENDED.');

echo ' World';

?>

Output (Screenshot6):-

die exit function
Screenshot6
In this example you can also echo out string or error in die functions keyword. Similarly you can do it with exit function as well.
Die and Exit functions are used in database connections. Let see it in example for connecting it with mysql database where ” mysql_connect(); ” function is used.

Code for Connect Mysql database (Screenshot7)

connect mysql database
Screenshot7
<?php



mysql_connect('localhost','root','') or die ('Could not connect to database.'');



echo 'connected!';



?>

output (Screenshot8):-

code for connection to mysql database
Screenshot8

In this example I have connected mysql database with my localhost of xampp. Now you don’t worry about connections of database which we will see it later on. Here we use function name ” mysql_connect(‘localhost’,'username’,'password’) ” where it depends on server my server which is localhost ,my username is root and my password is blank. Now in this example I have used die function if any of my variables like username, server , or password mismatch or database is not connected successfully it will show me error “Could not connect to database”.But here it is connected.

Code for connect mysql database with die or exit functions (Screenshot9):-

Code for connect mysql database with die or exit functions
Screenshot9

<?php



mysql_connect('localhost','alex','') or die ('Could not connect to database.'');



echo 'connected!';



?>

Output Screenshot10
connection failed mysql database
Screenshot10
In this example my username is mismatched because its “root” and I have changed it to “alex”, thus its not going to connect with database. You can see output with warning error or we can also echo out our own error i.e. “Could not connect to database”.

Code for connect mysql database with die or exit functions (Screenshot11):-

Screenshot11

<?php



@mysql_connect('localhost','alex','') or die ('Could not connect to database.'');



echo 'connected!';



?>


output (Screenshot12):-

connection with database
Screenshot12

In this example we just output our own error for that we just popin @ before function. So, This is user-friendly way to connecting to database or the killing page. In this we can also use exit function instead of die function. It will exactly to the same thing which we done with die functions.
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

For Loop in php


For Loop In Php Programming Languages. This tutorial is for “For Loop”. Now syntax of For Loop is bit complicated. Also,its structure is slightly different. If we make blogs related to For loop you must observe that its similar to While Loop and Do While Loop. However, in For Loop what we are doing is we use three expressions within this brackets.These three expressions are ,firstly to initialize the variable then control the loop by giving a definite condition to the variable and lastly increment the initialized value of variable.


Code for For loop (Screenshot1):-

For loop in php
Screenshot1

Output (Screenshot2):-

For Loop In Php
Screenshot2
In this example, I am firstly declaring variable “count” in For Loop,secondly evaluate whether condition is true or false i.e.count is greater than or equal to ten and lastly increment the value of count.In For Loop,”;” sign between these expressions is must.Second expression will only get executed after execution of first expression and third expression will only get executed after execution of second expression whenever any For Loop is called for the first time in program.But from second time onward it will only execute second and third expressions,since the initialization of variable is done while calling the For Loop for the first time.
Html tags should be in single inverted commas at echo function of php programming languages.

Code for For loop (Screenshot3):-

Loops
Screenshot3

Output (Screenshot4):-

for loop in php
Screenshot4
In this example, we are counting from 10 to 1 by decremented the value of variables.So, this is basic For Loop in php.
Read More

Do While Loops In Php

This tutorial is for Do While Loop.Now we had looked while loops that basically count to 10 and prints hello each time it is incremented. Do while loop is very similar to while loop,only a minor difference is that we don’t check whether condition is true or false or we don’t evaluate condition in start. Do while loop always runs the loop once.

Code for Do While Loop (Screenshot1):-

Do While Loops
Screenshot1

Output Screenshot2

Do While Loops In Php
Screenshot2
In this example we are using “do” keyword that will not check conditions but will perform whatever we tell to do and after that in while loop it will check condition. In above example, we have echo out in do loop “This will ALWAYS show” and in while statement we have false that condition.

Code for Do while loop (Screenshot3):-

Do while loops
Code for do while loops Screensot3

Output (Screenshot4):-

Screenshot4
In this example we are doing same thing as we did in while loops Examples .In this we are echoing out “This will ALWAYS show” 10 times where we set counter to start with value 1 and incremented it up-to value 10. If while loop condition get false it will always echo it out once by do loop. The main aim of using Do While Loop is that as it name refers it will first echo out once any statement/process declared inside “do statement” and only after executing that part it will move for executing “do statement” further on the basis of declared conditions in “while statement”.
Read More

Wednesday, 26 February 2014

While Loops in php

This tutorial is of While Loops. In php,why we need loops during programming ? Here we may repeat certain amount of actions for a specified amount of times.

 Code for While Loop (Screenshot1):-

While Loops in php
Screenshot1

Output (Screenshot2) :-

while php
Screenshot2


In this example we have echoed out “Hello” by while loop.In this loop 1 evaluates true which echo out “Hello” but what if we want to echo out “Hello” 100 times! Let see example,how to echo out “Hello” for 10 times.

Code for While Loop (Screenshot3):-

Code for while loops in php
Code for while Loops Screenshot3

Output (Screenshot4):-

php while code
Screenshot4
In this example, we have starter counter which initialize/starts loop and condition counter which must be less than or equal to 10 that will be incremented again and again upto the limit of condition counter and thus echo out “Hello” 10 times.

Code for While loop (Screenshot5):-

While Loops
Screenshot5

Ouput (Screenshot6) :-

loops
Screenshot6
In this example condition will false and will not display “Hello” because condition counter is greater than 10.
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