APHP NOTES
APHP NOTES
<?php
$txt = "Hello World!";
$number = 16;
?>
PHP Operators
Assignment Operators
Arithmetic Operators
Comparison Operators
String Operators
Combination Arithmetic &
Assignment Operators
Assignment Operators
Assignment operators are used to set a variable
equal to a value or set a variable to another
variable's value.
$my_var = 4;
$another_var = $my_var;
Arithmetic Operators
Operator Meaning Example
+ Addition 2+4
- Subtraction 3-1
* Multiplication 5*2
Division 10/5
/
Modulus 23/10
%
Example
<?php
$add= 2 + 4;
$sub = 6 - 2; $multi = 5 * 3; $div = 15 / 3; $mod = 5 % 2;
echo " Addition: 2 + 4 = ".$add."<br />";
echo “ subtraction: 6 - 2 = ".$sub."<br />";
echo “multiplication: 5 * 3 = ".$multi."<br />";
echo “division: 15 / 3 = ".$div."<br />";
echo “ modulus: 5 % 2 = $mod;
?>
DISPLAY: Addition: 2 + 4 = 6
subtraction: 6 - 2 = 4
multiplication: 5 * 3 = 15
division: 15 / 3 = 5
modulus: 5 % 2 =1
Comparison Operators
Comparisons are used to check the relationship
between variables and/or values.
Example: $x = 4 and $y = 5;
Operator Meaning Example Result
== Equal To $x == $y False
+= Plus Equals $x += 2; $x = $x + 2;
-= Minus Equals $x -= 4; $x = $x - 4;
*= Multiply Equals $x *= 3; $x = $x * 3;
/= Divide Equals $x /= 2; $x = $x / 2;
%= Modulo Equals $x %= 5; $x = $x % 5;
Concatenate $my_str =
.= $str.="hello";
Equals $my_str . "hiii";
Constants
A constant is a identifier for a simple value.
Constants also follows the rules of naming
variables.
<?php
Define(“constant_var”, “value”);
?>
Constant Rules
Constant do not have $ sign.
It may only be defined using define() function.
It can not be redefined or undefined once they
have been set.
Constants may only evaluate to scalar values.
Example
<?php
define("MAXSIZE", 100);
echo "Declared Value:".MAXSIZE."<br>";
echo "Retrieve Constant
Value:".constant("MAXSIZE"); // same thing
as the previous line
?>
Display : Declared Value:100
Retrieve Constant Value:100
Expressions
Expressions are the most important building
stone in PHP.
The most basic forms of expressions are
Constant & Variables.
The common expressions are COMPARISION
operator.
This will give you either TRUE or FALSE
value.
PHP supports >,<,<=,>=,==,!=,===,!==
w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists
fread() function
This function is used to read the content in the
opened file
fread(‘opened file name’,size);
Ex:
Fread($file,100);
Find the full document size
$size=Sizeof($filename);
Fread($file,$size);
fwrite()
This function is used to write the content in the
opened document.
fwrite(‘openened file name’,content);
Ex
fwrite($file,‘cegonsoft’);
CLOSING A FILE
The fclose() function is used to close an open
file:
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
CHECK END-OF-FILE
The feof() function checks if the "end-of-file"
(EOF) has been reached.
The feof() function is useful for looping
through data of unknown length.
Note: You cannot read from files opened in w,
a, and x mode!
EXAMPLE:
if (feof($file)) echo "End of file";
READING FILE LINE BY LINE
The fgets() function is used to read a single
line from a file.
<?php
$file = fopen("welcome.txt", "r") or
exit("Unable to open file!");
//Output a line of the file until the end is
reached
while(!feof($file))
{ echo fgets($file). "<br />"; }
fclose($file);?>
READING FILE BY CHARACTER
The fgetc() function is used to read a single
character from a file.
<?php
$file=fopen("welcome.txt","r") or
exit("Unable to open file!");
while (!feof($file))
{ echo fgetc($file); }
fclose($file);?>
COOKIES
A cookie is often used to identify a user.
A cookie is a small file that the server embeds
on the user's computer.
Each time the same computer requests a page
with a browser, it will send the cookie too.
With PHP, you can both create and retrieve
cookie values.
CREATE COOKIE
The setcookie() function is used to set a
cookie.
setcookie(name, value, expire, path, domain);
EXAMPLE:
<?php setcookie(“cookie_name",
“cookie_value", time()+3600);?>
The value of the cookie is automatically
URLencoded when sending the cookie, and
automatically decoded when received
RETRIVE COOKIE
The PHP $_COOKIE variable is used to
retrieve a cookie value.
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
DELETE COOKIE
When deleting a cookie you should assure that
the expiration date is in the past.
<?php
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
?>
SESSIONS
Before you can store user information in your
PHP session, you must first start up the
session.
<?
php session_start();
?>
The code above will register the user's session
with the server, allow you to start saving user
information, and assign a UID for that user's
session.
STORE & RETRIVE
<?php
session_start();
// store session data
$_SESSION['views']=1;
?><html><body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?></body></html>
The isset() function checks if the "views" variable has
already been set. If "views" has been set, we can
increment our counter. If "views" doesn't exist, we
create a "views" variable, and set it to 1:
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>
DESTROY SESSION
If you wish to delete some session data, you
can use the unset() or the session_destroy()
function.
The unset() function is used to free the
specified session variable:
<?php
unset($_SESSION['views']);?>
<?php
session_destroy();?>
DATE FUNCTIONS
The PHP date() function is used to format a
time or a date.
SYNTAX:
date(format,timestamp);
Format:Required. Specifies the format of the
timestamp
Timestamp: It is the number of seconds since
January 1, 1970 at 00:00:00 GMT. This is also
known as the Unix Timestamp.
The first parameter in the date() function
specifies how to format the date/time.
d - The day of the month (01-31)
m - The current month, as a number (01-12)
Y - The current year in four digits
<?php
echo date("Y/m/d");echo "<br />";
echo date("Y.m.d");echo "<br />";
echo date("Y-m-d");?>
The mktime() function returns the Unix
timestamp for a specified date.
SYNTAX:
mktime(hour,minute,second,month,day,year)
EXAMPLE:
<?php
$tomorrow =
mktime(0,0,0,date("m"),date("d")
+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d/",
$tomorrow);?>
FILE UPLOADING
With PHP, it is possible to upload files to the
server.
<form action="uploadfile.php" method="post“
enctype="multipart/form-data">
<label for="file">Filename:</label>
OR Filename: <input type="file">
The enctype attribute of the <form> tag specifies
which content-type to use when submitting the form.
"multipart/form-data" is used when a form requires
binary data, like the contents of a file, to be uploaded