PHP Basic: What You Should Already Know
PHP Basic: What You Should Already Know
in
[email protected], [email protected]
PHP BASIC
1. PHP is a powerful server-side scripting language for creating
dynamic and interactive websites.
2. PHP is the widely-used, free, and efficient alternative to
competitors such as Microsoft's ASP. PHP is perfectly suited for
Web development and can be embedded directly into the HTML
code.
3. The PHP syntax is very similar to Perl and C.
4. PHP is often used together with Apache (web server) on various
operating systems. It also supports ISAPI and can be used with
Microsoft's IIS on Windows.
What You Should Already Know
HTML
Some scripting knowledge (Like VB Script, Java Script)
What is PHP?
What is MySQL?
MySQL is a database server
MySQL is ideal for both small and large applications
MySQL supports standard SQL
MicroDots Computers Education,
103,Royal Buildings, Sathy Road,
Near Bus Stand Roundtana Signal,
Erode. Ph: 2224404 Cell: 97888 73008
www.edu.microdots.in
[email protected], [email protected]
Why PHP?
PHP runs on different platforms (Windows, Linux, Unix, etc.)
PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
PHP is FREE to download from the official PHP resource:
www.php.net
PHP is easy to learn and runs efficiently on the server side
PHP Syntax
<?php and ends with ?>.
Comments in PHP
1. In PHP, we use // to make a single-line comment
2. /* and */ to make a large comment block.
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
PHP Variables
Variables are used for storing values.
Variables in PHP
1. All variables in PHP start with a $ sign symbol.
The correct way of setting a variable in PHP:
Syntax:
$var_name = value;
Example:
<?php
$txt = "Hello World!";
$number = 16;
?>
Variable Naming Rules
1. A variable name must start with a letter or an underscore "_"
PHP constant:
Example:
<?php
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line
?>
There is no need to write a dollar sign ($) before a constant, where as in Variable one
has to write a dollar sign.
Constants cannot be defined by simple assignment, they may only be defined using
the define() function.
Constants may be defined and accessed anywhere without regard to variable scoping
rules.
Once the Constants have been set, may not be redefined or undefined.
Name Description
__LINE__ The current line number of the file.
The full path and filename of the file. If used inside an include,the name of the included file
__FILE__ is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older
versions it contained relative path under some circumstances.
__FUNCTION__ The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as
__CLASS__
it was declared (case-sensitive). In PHP 4 its value is always lowercased.
The class method name. (Added in PHP 5.0.0) The method name is returned as it was
__METHOD__
declared (case-sensitive).
PHP String
A string variable is used to store and manipulate a piece of text.
Strings in PHP
<?php
$txt="Hello World";
echo $txt;
?>
The Concatenation Operator
1. The concatenation operator (.) is used to put two string values
together.
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>
Operators
Arithmetic Operators
Comparision Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Arithmetic Operators:
Examples:
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addtion Operation Result: $c <br/>";
$c = $a - $b;
echo "Substraction Operation Result: $c <br/>";
$c = $a * $b;
echo "Multiplication Operation Result: $c <br/>";
$c = $a / $b;
echo "Division Operation Result: $c <br/>";
$c = $a % $b;
echo "Modulus Operation Result: $c <br/>";
$c = $a++;
echo "Increment Operation Result: $c <br/>";
$c = $a--;
echo "Decrement Operation Result: $c <br/>";
MicroDots Computers Education,
103,Royal Buildings, Sathy Road,
Near Bus Stand Roundtana Signal,
Erode. Ph: 2224404 Cell: 97888 73008
www.edu.microdots.in
[email protected], [email protected]
?>
</body>
</html>
Comparison Operators: (Relational Operators)
Example:
<html>
<head><title>Comparision Operators</title><head>
<body>
<?php
$a = 42;
$b = 20;
if( $a == $b ){
echo "TEST1 : a is equal to b<br/>";
}else{
echo "TEST1 : a is not equal to b<br/>";
}
if( $a > $b ){
echo "TEST2 : a is greater than b<br/>";
}else{
echo "TEST2 : a is not greater than b<br/>";
}
if( $a < $b ){
echo "TEST3 : a is less than b<br/>";
MicroDots Computers Education,
103,Royal Buildings, Sathy Road,
Near Bus Stand Roundtana Signal,
Erode. Ph: 2224404 Cell: 97888 73008
www.edu.microdots.in
[email protected], [email protected]
}else{
echo "TEST3 : a is not less than b<br/>";
}
if( $a != $b ){
echo "TEST4 : a is not equal to b<br/>";
}else{
echo "TEST4 : a is equal to b<br/>";
}
if( $a >= $b ){
echo "TEST5 : a is either grater than or equal to b<br/>";
}else{
echo "TEST5 : a is nieghter greater than nor equal to b<br/>";
}
if( $a <= $b ){
echo "TEST6 : a is either less than or equal to b<br/>";
}else{
echo "TEST6 : a is nieghter less than nor equal to b<br/>";
}
?>
</body>
</html>
Logical Operators:
Example:
<html>
<head><title>Logical Operators</title><head>
MicroDots Computers Education,
103,Royal Buildings, Sathy Road,
Near Bus Stand Roundtana Signal,
Erode. Ph: 2224404 Cell: 97888 73008
www.edu.microdots.in
[email protected], [email protected]
<body>
<?php
$a = 42;
$b = 0;
if( $a && $b ){
echo "TEST1 : Both a and b are true<br/>";
}else{
echo "TEST1 : Either a or b is false<br/>";
}
if( $a and $b ){
echo "TEST2 : Both a and b are true<br/>";
}else{
echo "TEST2 : Either a or b is false<br/>";
}
if( $a || $b ){
echo "TEST3 : Either a or b is true<br/>";
}else{
echo "TEST3 : Both a and b are false<br/>";
}
if( $a or $b ){
echo "TEST4 : Either a or b is true<br/>";
}else{
echo "TEST4 : Both a and b are false<br/>";
}
$a = 10;
$b = 20;
if( $a ){
echo "TEST5 : a is true <br/>";
}else{
echo "TEST5 : a is false<br/>";
}
if( $b ){
echo "TEST6 : b is true <br/>";
}else{
echo "TEST6 : b is false<br/>";
}
if( !$a ){
echo "TEST7 : a is true <br/>";
}else{
echo "TEST7 : a is false<br/>";
}
if( !$b ){
echo "TEST8 : b is true <br/>";
}else{
echo "TEST8 : b is false<br/>";
}
?>
</body>
</html>
Assignment Operators:
Example:
<html>
<head><title>Assignment Operators</title><head>
<body>
<?php
$a = 42;
$b = 20;
?>
</body>
</html>
Conditional Operator
Example:
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
$a = 10;
$b = 20;