Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

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

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

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

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

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

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