Sunday, June 30, 2013

php fopen and how to use it

php fopen function and how to use it with files
In this lesson we will talk about how to deal with files in php, first let's create a text file and name it "text.txt", write whatever you want in it 

php fopen

After that we will use php fopen like this 
<?php
$file=fopen("text.txt",'r');
?>
The fopen function have two parameter
fopen("first parameter","second parameter") 
first parameter: we put the name of the file or the link of it

second parameter: we put the way that we want to use to open the file
There is many ways to open a file
"r": to open a file for only reading start at the begining of the file
"w": to open a file and write in it at the beginning or create a new file if its not exist, this method will erase all the data in the file
"a": to open a file and write in it at the end of it or create a new file if it's not exist
"r+": to read or write in the file start at the begining of the file
"w+": to read or write in the file with erase all the data or create a new file if it's not exist
"a+": to read or write in the file with keeping all the data in that file start at the end of the file

Let's continue the last example
If we write the last example like this
<?php
$file=fopen("text.txt",'r');
echo "$file";
?>
We'll see this on the screen
php fopen
But we should have Hi how are you! because this is what we wrote in the file, so how?
to print the content of the file we must write this code using php fgets function

<?php
$file = fopen("text.txt", "r");
$text=fgets($file);
echo "$text";
fclose($file);
?>
And the result is
php fopen
As you see we use three functions (fopen,fgets,fclose)
In the first line we asking PHP to open up the file and in the second line we use the fgets function and what it does is to get the a line of the file, and we store it in the text variable $text 
After that we print the variable $text and close the file with the function fclose

I think you are wondering now if we have a file with two or three lines how we can print theme all in the screen, well there are loop's in PHP and we will use while loop to solve this problem keep reading and you will know how
<?php
$file = fopen("text.txt", "r");
while (!feof($file)) {
$line = fgets($file);
print $line . "<BR>";
}
fclose($file);
?>

Let's explaine the code 
we asking PHP to open the file 
we put a loop and a condition in it !feof($file)  feof is a function that tells PHP when the end of a file has been reached but we use the not ! operator 
so this code while (!feof($file)) is like we said while the end of the file has been not reached keep looping
So we get a line of text from our file, and then place the line into a variable. We then print out the line of text
print $line . "<BR>"; this line is to printing out the line of text, we're adding a HTML line break
fclose($file); After the loop is finished we close the file

This was the php fopen and other functions, I hope you learn something today please share this lesson with your friends if you like it
See you next time.

Read More

Thursday, June 27, 2013

include php in html

include php in html
In this lesson we will talk about include function and how to use it, so let's go
First include function are used to insert a php file into an html file or another php file and it will be executed with the rest of the code in the same file, let's do an example
<?php
$a=15;
$b=10;
if($a==$b){
include ('h1yes.php');
}
else{
include ('h1no.php');
}
?>

The code in the h1yes.php is
<?php
echo "<h1> YES</h1>";
?>

The code in the h1no.php is
<?php
echo "<h1> NO </h1>";
?>

The files must be in the same directory in this example

include php in html

As we can see the values ​​of variables are not equal so we will see no on the screen

include php in html

So this is how to include php in html, there is an other function to include php in html and it's the require function and the difference between it and the include function is that the code will be stop if there is an error in finding the file with the require function.

But with the include function the code will be continue even if there is an probleme to finding the file included 
That was the include php in html lesson, See you next time.

Read More

Wednesday, June 26, 2013

php simple programs - 1st degree equation solver

php simple programs
In this lesson we will take a php simple program which can be used to solve 1st degree equations based on what we have learned so far,Let's go
This is the whole code

<style type="text/css">
#e1 {
border:medium;
float:center;
background-color:#0C3;
border-radius:10px;

}
#sol {
background-color:#999;
float:center;
border-radius:10px;
margin:250px;
margin-top:5px;
}



</style>


<div id="e1">
<center>
<h1>this program solve 1sd degree equations ax+b=0</h1>
<form action="2ndequation.php" method="post" >
 a= <input name="n1" type="number" />
 b= <input name="n2" type="number" />
<input name="submit" type="submit" value="calculate"  />
 </form>
 </center>
 </div>
 <div id="sol">
 <center>
 <?php
if(isset($_POST['submit'])){
$a=$_POST['n1'];
$b=$_POST['n2'];
if($a==0 && $b==0){
echo "the variable have infinite values";
}else if($a==0){
echo "no solutions";
}else{
$x=(-$b/$a);
echo "the solution of equation is x="."$x";
}

}
?>
</center>
</div>

The result will be like this:

php simple programs


Now let's explain it
From <style type="text/css"> To </style> is a CSS code if you want a section about CSS just leave a comment below
Just after it we define a block and give it an ID if you want a section about HTML just leave a comment below

We put <center> to place the block in the middle of the page
We add a title and define a form which will be used to input the values of parameters a and b and a submit buton and after it we close the block

we start a new block and put in it the php code
we use an condition to verify if the buton are pressed with isset function, so if the buton pressed it will do the code between brackets with the red color

We define two variables a and b they will receive the values from the form with POST function
The rest of the code is a math logic conditions I think it's easy to understand however if you find any probleme just leave me a comment and I will help you OK

I hope you enjoy and understand the php simple programs lesson, if you like it share it with your freind
See you next time.

Read More

Tuesday, June 25, 2013

php date time

php date time 
In this lesson we will talk about php date time and how to use time and date in php, we will use a function
called date and this function require three or six parameters days,months,years,hours,minutes,seconds
<?php
echo date("D/M/Y H:i:s");
?>
And the result is
php date time
As you see D for days, M for months and Y for years, H for hours, i for minutes and s for seconds
Now let's use lowercase letters
<?php
echo date("d/m/y h:i:s");
?>
And the result is
php date time
I think you understand the difference, but if you notice I let i and s lowercase why? try it and you will know 
This was the php date time I hope you understand it.
See you next time.


Read More

php post get

php post get
This lesson will be short be cause i will talk about the php post get and the difference between the post function and the get function, and we will use the form of the last lesson php forms example
The code was like this
<?php
$username= $_POST['user']
$password= $_POST['password']
echo "My username is $username and my password is $password";
?>
<form action="<?php echo $PHP_SELF; ?>" method="post" >
<input name="user" type="text" value="usernam" />
<input name="password" type="password" value="password" />
<input name="submit" type="submit" value="Log in"  />
 </form>
And the result was:
php post get
Now let's use the GET function and see what's the difference 
<?php
$username= $_GET['user']
$password= $_GET['password']
echo "My username is $username and my password is $password";
?>
<form action="<?php echo $PHP_SELF; ?>" method="get" >
<input name="user" type="text" value="usernam" />
<input name="password" type="password" value="password" />
<input name="submit" type="submit" value="Log in"  />
 </form>
The result 
php post get
The same result but look at the url, the GET function shows my username and my password unlike the POST function 
Notes:
  • We should always use the POST function because it safe
  • When you use the POST function the method must be post method="get"
That's it for this lesson php post get 
I hope you learn something useful, see you next time.





Read More

Sunday, June 23, 2013

php form example - php forms

php form example
What is an form in php? actually the forms are in html not in php but when we put a form in html the php handle all the operations about that form
For example you want to enter your facebook account what you should do?
Of course you must enter your email and your password into the form and hit Log in, but who tell's facebook that you are the right person, well it's the php language, you want to know how? just keep reading
In Our level we will make a form and receive values from it and them them on the screen.
Let's do this!
<form action="" method="post" >
<input name="user" type="text" />
<input name="password" type="password" />
<input name="submit" type="submit" value="Log in"  />
 </form>
This is the form and you will see this on the screen
php form example

If we write it like this
<form action="" method="post" >
<input name="user" type="text" value="usernam" />
<input name="password" type="password" value="password" />
<input name="submit" type="submit" value="Log in"  />
 </form>
we will see this 
php form example

So the difference as you see is the "username" and the "........." and we add this to let the user know where to put his username and password, Now let's write the php code

<?php
$username= $_POST['user']
$password= $_POST['password']
echo "My username is $username and my password is $password";
?>
<form action="<?php echo $PHP_SELF; ?>" method="post" >
<input name="user" type="text" value="usernam" />
<input name="password" type="password" value="password" />
<input name="submit" type="submit" value="Log in"  />
 </form>

In the first line we ask the php to get the value from the form which named user
In the second line we ask the php to get the value from the form which named password
In the third line we ask the php to print the expression with the values of the two variables $username and $password
Always write this <?php echo $PHP_SELF; ?> in the action if you are writing the php code in the same file of the form
Always use the post method because it's safe and we will talk about the difference between post and get later
After this big explanation let's see the result 
php form example
This was an php form example I hope you learn something useful today
See you next time


Read More

Friday, June 21, 2013

php functions

php functions
what is a function in php?
A function in php is a set of codes that we want to execute it.:
function name of function(parameter)
{
what we want to do
}
The parameter is like a variable, you will understand when we do the example.
But what is the importance of a function? To know that lets do an example.
We want to create a function to calculate the sum of two numbers (just an example) the code is:

<?php
function snumbers($n1,$n2)
{
echo "$n1"+"$n2"; 
}
snumbers("5","10"); we just type the name of function and the value of parameters
?>

We will see 15 on the screen, so the importance of php functions is if we need to execute the code many times we don't need to rewrite the code, we just write it one time in a function and when we need it we just call the function name and the code will be executed.
This was the php functions lesson I hope you enjoy it.

And remembre that the real power of php language comes from it's functions

see you next time.

Read More

Thursday, June 20, 2013

php for loop - foreach php

php for loop
In this lesson we will talk about php for loop, there are fore type of loops in php
while,for,do..while and foreach we will talk about for loop in this lesson
for (init, condition, increment)
{
what we want to repeat;
}
init: the variable and it's first value
condition: if it's true the loop continue and if it false the loop stop
increment: the value to be added to the counter
Let's say that we want to print numbers from 1 to 10, we can write the code like this
<?php
echo '1';
echo '2';
.
.
.
echo '10';
?>
And if we using php for loop we will write the code like this
<?php
$i=1;
for($i=1; $i<=10; $i++)
{
echo "$i"." ";
}
?>
We will see 1 2 3 4 5 6 7 8 9 10 on the screen
we use this ." " to put distances between numbers
This was the php for loop, now let's move to the foreach php loop
foreach ($array as $value)
{
what we want to do;
}
foreach php loop using with arrays
Let's define four values in one array and try to print theme on the screen, the code will be like this
<?php
$numbers = array('one','four','nine','twelve');
foreach($numbers as $value)
{
echo "$value"." ";
}
?>
We will see this on the screen
foreach php


This was the foreach php loop i hope you enjoy the lesson
we will talk about while and do..while, when we get to database lesson, if you have any question just leave a comment.
see you next time.




Read More

Tuesday, June 18, 2013

php arrays

php arrays
What is an array, array in php is a variable which can store many values, there are three types of php arrays

  • Numeric array : array with a numeric index
  • Assosiative array : array where their values indexed with specific ID
  • Multidimensional array : array containing many arrays
We can define php arrays like this,
$names = array("john","lora","maria");
In this example we define an array called "names" and we store three values in it, if we want to print one of the values for example the first one we should write this code
echo $names[0];
We should see john on the screen and as you notice the index of the first value is 0, what we should write if we want to print the third value
echo $names[2];
php arrays
This was the explanation of the Numeric array, now let's talk about Assosiative array
We can define Assosiative array like this
$names = array("j"=>"john","l"=>"lora","m"=>"maria");
In this example we define an Assosiative array with three values and each value have a key 
Let's see how we can call the values in this array

echo $names["j"];
echo $names["l"];
echo $names["m"];
We will see this in the screen
johnloramaria

Now let's talk about the third type of php arrays the Multidimensional array, let's take an example

$names = array (
"name1"=>array("john","julia"),"name2"=>array("lora","lisa"),"name3"=>array("maria")
);
The global array called names and inside of it there are three arrays name1,name2,name3 every array from these arrays have diffrent values, so if we want to print the first value of the first array we should write this
echo $names['name1'][0];
And if we want to print the second value of the second array we should writs this
echo $names['name2'][1]

This was the php arrays lesson i hope you enjoy it, if you have any question just leave a comment
See you next time.

Read More

Monday, June 17, 2013

php switch statment

php switch
In this lesson we will talk about switch statment and what does it mean and what it can do, before we start write the code let us give an example first, we want to make a calculator that can do this operations (+,-,*,/)
we can write the code like this
<?php
$n1 = 5 ;
$n2 = 7 ;
$operation = "/" ;
$result = " " ;
if ( $operation == '+') { 
echo "$n1" +  "$n2" ;
}
 if ( $operation == '-') { 
echo "$n1" -  "$n2" ;
}
if ( $operation == '*') { 
echo "$n1" *  "$n2" ;
}
if ( $operation == '/') { 
echo "$n1" /  "$n2" ;
}
?> 
We will see on the screen 12, But if we use php switch statment it will be like this
<?php
$n1 = 5 ;
$n2 = 7 ;
$operation = "+" ;

switch ($operation) { 
case "+" :
echo "$n1"+"$n2" ;
break;
case "-" :
echo "$n1"-"$n2" ;
break;
case "*" :
echo "$n1"*"$n2" ;
break;
case "/" :
echo "$n1"/"$n2" ;
break;
}
?>
php switch

And we will see 12 in the screen but without using if else statement, This was the php switch statement
 I hope you enjoy the lesson, and see you next time
Read More

php if else

php if else
In this lesson we will talk about php if else and what does it mean and what we can do with it
so if statement are using to verify a particular condition after it, for example
if you are not busy bring me a cup of coffee, the condition in this case is not busy, if it is true than he will bring you a cup of coffee,if not he will beat you hhhhhh just kidding he will not bring you the coffee cup,
So let's write some code

<?php
$p = "busy" ;
if ($p == "busy") {
echo "sorry i can't i'm busy" ;
}
?>

In this example we define a variable and we gave him the value of busy, and put a conditional statement after it to check if the variable p is equal to busy then he will print sorry i can't i'm busy 
So write this code and you will see in the screen this statment sorry i can't i'm busy 

Let's do other example with if and else
<?php
$p = "not busy" ;
if ($p == "busy") {
echo "sorry i can't i'm busy" ;
}
else {
echo "ok no probleme" ;
}
?>
In this example the program will check if the variable p equal to busy, but as we see it's not so he will scroll down to the else statement and do what is between brackets

php if else


I hope you understand this lesson, php if else
any question just leave a comment
See you next time.

php switch
Read More

Sunday, June 16, 2013

php variables - some operations

In this lesson we will talk about operations in php and take some examples, Ready let's go
EXAMPLE 01:
<?php
$n1 = "12" ;
$n2 = "4" ;
echo "$n1" + "$n2" ;
?>
You should see 16 on the screen

EXAMPLE 02:
<?php
$n1 = "12" ;
$n2 = "4" ;
echo "$n1" * "$n2" ;
?>
You should see 48 on the screen, easy don't you think
When we have a good base in php language we will make interactive programs like calculator and games
Just be patient



Read More

php variables

php variables
In this lesson I will talk about variables in php, and firstly i will describe what is a variable in php
A variable in php start with $ sign and the name of the variable

For example $pass = 12;
In this example we have define a variable calls pass and we gave him a value of 12, so if we write
echo "pass";

what we will see in the screen?

To find out write this code in a new document or the same of the previous lesson
<?php
$pass = "12" ;
echo "$pass" ;
?>
go to your navigator and type localhost/name.php you should see some thing like this


you see 12 in the screen good, but have you wonder if we write echo '$pass' ; instead of echo "$pass" ;
what is the difference between " and '
well let's try
As you see we have $pass instead of 12, so the difference between them is 
" give us the value of the variable
' give us the word as it is 

In the next lesson we will learn some operation to use with php variables.

Read More

php sample programs - the syntax

php sample programs:
Today we will start our first program but we must have a little knowledge of html, some basics things that's all, so let's go
Always the php code start with this tag <?php and ended with this one ?>

Now let's write some php sample programs
Open the editor and write this code

<?php
echo "HELLO WORLD" ;
?>

Save the file in www folder, you will find it in appserv folder, the name of file should be name.php
Open your navigator and write localhost



After localhost /name.php you should see HELLO WORLD


php sample programs

echo: this is an order that tells php compiler to show some thing we want
We can use ' instead of " but just in this case we will talk about the difference between them later
In the next lesson we will talk about variables in php and write other php sample programs.
have a good time

Read More

HI

Hello and welcome to my blog 
In this blog we will talk about php programming from A to Z, and i hope you enjoy what i give to you
and we will focus on php knowledge base first and we will move on.
Read More

php knowledge base - what you need to start?

php knowledge base
In this lesson i will talk about all things that you need to start creating php codes and the first thing you will need is a local host and what does this mean, well its mean that you need a server to compile php codes because php is a server side language so instead of you get a host and put your php files on it you will make your computer as a server with this program.

Appserv and you can download it from this url:
 http://prdownloads.sourceforge.net/appserv/appserv-win32-2.5.10.exe?download

php knowledge base




Also, you need an editor like Notepad++ or Dream weaver or other editor it's your choise

Did you download and setup the programs?
if yes then congratulations and welcome to php world
In the next lesson we will start learning the basics of php "php knowledge base"
Have a good time.



Read More