Showing posts with label program. Show all posts
Showing posts with label program. 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

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

Thursday, 13 February 2014

More On Error Reporting in php

Error Reporting
Error Reporting
This is a tutorial for Error reporting. Let’s Back to php.ini file(ScreenShot1).

More On Error Reporting in php
Screenshot1
Error Reporting value is E_All, So we are Reporting errors back to our self as we developing. What happen if i want to report errors Strict errors. Now E_Errors Does not Include E_Strict, So i will specify under error_reporting another value E_Strict (ScreenShot2).

error reporting
Screenshot2

error_reporting = E_ALL & E_STRICT


Now this will include errors of run-time notices, enable to have PHP suggest changes to your code which will ensure the best interchangeability and forward compatibility of your code. So basically giving us messages about code itself other then errors that you made be coming across  Now “&” sign is use for including and for not including “~” sign is used. (Screenshot3).

php code errors
Screenshot3

error_reporting = E_All & ~E_NOTICE 

If we don’t wanna Show E_NOTICE types of error we include “~”.This will show all errors, except for notice and coding standards warnings.So Its upto you what level errors you want to display if your codes having errors.If your application is big you can turn onhave a look. Now when u complete your program or applicationon web-server you have to turn error_reporting off
When u complete your program you need to :-

error_reporting=0;

Why the reason being is the errors shows on your pages specially in E_STRICT notices you don’t want to your users or viewers to tell about your errors notices and warnings. For security reasons also u had to turn off. For example if u including file or copying file and there u had errors its shows errors to visitors and they will understand your website structure.So, Best thing is to you put error_reporting=0 when u release your website to web-server to visitors. Now if u had paid hosting you will not have access to php.ini files for that there are some functions have look. What happen if we want to dynamically update error_reporting value inside php (Screenshot4), and (Screenshot5).

php codes error reporting
Screenshot4
error_reporting
Screenshot5
 Code Dynamically update error_reporting value inside php:-

Code Error reporting for E_ALL :-


<?php

error_reporting(0);

echo $var = 'Alex'

$var2 = 'billy';

?>




You can also use functions for writing dynamically update error_reporting value inside php

ini_set function
Code for ini_set function Screenshot6

<?php

ini_set('error_reporting',E_ALL);



echo $var = 'Alex'

$var2 = 'Billy';

?>


This is all About error_reporting how u can put errors for you how can you find errors for visitors.
Read More

Comments in php

comments php
Comments In php
This is a tutorial for comments inside php. In php or any program you use you would be knowing how important is commenting in codes to understand to find proper codes easily there are two types of commenting methods one is single line comment and second is multiple line comments. comments help us and other programmer what program does and where its going.We can write note for copy-write reasons. If we pass our codes to other coder or for modification by other programmer comments are useful to explain them what we want to do actually

Example for single line comments (Screenshot1):-


Comments in php
Example for single line comments (Screenshot1)
<?php
//Uses echo language construct  to output the text "Hello World." to the Browser
//echo 'Hello world.";

?>

output(Screenshot2) :-




Comments in php
Screenshot2
In this codes we had comments codes and out comment both in “//” so will not display in browser it will only show to the coded file which is useful only for programmer to see program flow. we can also comments codes which had error but later useful we can comment them.

Example for single line comments (Screenshot3):-

Example for single line comments
Example for single line comments (Screenshot3):-
<?php
//Uses echo language construct to output the text "Hello World." to the Browser
echo 'Hello world.";

?>


Output (Screenshot4):-


Comments in php
Output (Screenshot4)

Example for multiline Comments (Screenshot5):-

Example for multiline Comments
Example for multiline Comments (Screenshot5):-
<?php
/*
Author:Mohammed
Date:7/3/2013
*/
//Uses echo language construct to output the text "Hello World." to the Browser
echo 'Hello world.";
?>


Output (Screenshot6):-

Comments in php
Output (Screenshot6):-
Mutliline comments is used for note in our codes.
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