0% found this document useful (0 votes)
104 views

PHP Basic: What You Should Already Know

The document provides information about PHP including what PHP is, how it is used for web development, PHP syntax, variables, constants, strings, and operators. PHP is a server-side scripting language used to create dynamic websites that is free, efficient, and compatible with many databases and servers. It discusses PHP files, variables, constants, strings, and common operators used in PHP.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

PHP Basic: What You Should Already Know

The document provides information about PHP including what PHP is, how it is used for web development, PHP syntax, variables, constants, strings, and operators. PHP is a server-side scripting language used to create dynamic websites that is free, efficient, and compatible with many databases and servers. It discusses PHP files, variables, constants, strings, and common operators used in PHP.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

www.edu.microdots.

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?

PHP stands for PHP: Hypertext Preprocessor


PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix, Oracle, Sybase,
Solid, PostgreSQL, Generic ODBC, etc.)
PHP is an open source software
PHP is free to download and use

What is a PHP File?


PHP files can contain text, HTML tags and scripts
PHP files are returned to the browser as plain HTML
PHP files have a file extension of ".php", ".php3", or ".phtml"

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]

MySQL compiles on a number of platforms


MySQL is free to download and use
PHP + MySQL
PHP combined with MySQL are cross-platform (you can develop in
Windows and serve on a Unix platform)

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 ?>.

PHP Program Structure with HTML:


<html>
<body>
<?php
Body of the statements;
?>
</body>
</html>
Note:
1. Each code line in PHP must end with a semicolon.
2. There are two basic statements to output text with PHP: echo
and print.
3. The file must have the .php extension. If the file has a .html
extension, the PHP code will not be executed.

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]

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 "_"

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]

2. A variable name can only contain alpha-numeric characters and


underscores (a-z, A-Z, 0-9, and _ )
3. A variable name should not contain spaces. If a variable name is
more than one word, it should be separated with underscore
($my_string), or with capitalization ($myString)

PHP constant:

Example:

<?php
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line
?>

Differences between constants and variables are:

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.

Valid and invalid constant names:


// Valid constant names
define("ONE", "first thing");
define("TWO2", "second thing");
define("THREE_3", "third thing")
// Invalid constant names
define("2TWO", "second thing");
define("__THREE__", "third value");

PHP Magic constants:

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

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]

name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

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).

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]

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;
?>

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]

Operators
Arithmetic Operators
Comparision Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators

Arithmetic Operators:

Operator Description Example


+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiply both operands A * B will give 200

/ Divide numerator by denumerator B / A will give 2

Modulus Operator and remainder of after an


% B % A will give 0
integer division

Increment operator, increases integer value by


++ A++ will give 11
one

Decrement operator, decreases integer value by


-- A-- will give 9
one

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)

Operator Description Example


Checks if the value of two operands are equal
== (A == B) is not true.
or not, if yes then condition becomes true.

Checks if the value of two operands are equal


!= or not, if values are not equal then condition (A != B) is true.
becomes true.

Checks if the value of left operand is greater


> than the value of right operand, if yes then (A > B) is not true.
condition becomes true.

Checks if the value of left operand is less than


< the value of right operand, if yes then condition (A < B) is true.
becomes true.

Checks if the value of left operand is greater


>= than or equal to the value of right operand, if (A >= B) is not true.
yes then condition becomes true.

Checks if the value of left operand is less than


<= or equal to the value of right operand, if yes (A <= B) is true.
then condition becomes true.

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:

Operator Description Example


Called Logical AND operator. If both the
and operands are true then then condition becomes (A and B) is true.
true.

Called Logical OR Operator. If any of the two


or operands are non zero then then condition (A or B) is true.
becomes true.

Called Logical AND operator. If both the


&& operands are non zero then then condition (A && B) is true.
becomes true.

Called Logical OR Operator. If any of the two


|| operands are non zero then then condition (A || B) is true.
becomes true.

Called Logical NOT Operator. Use to reverses


the logical state of its operand. If a condition is
! !(A && B) is false.
true then Logical NOT operator will make
false.

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/>";
}

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>

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]

Assignment Operators:

Operator Description Example


Simple assignment operator, Assigns
= values from right side operands to left C = A + B will assigne value of A + B into C
side operand

Add AND assignment operator, It adds


+= right operand to the left operand and C += A is equivalent to C = C + A
assign the result to left operand

Subtract AND assignment operator, It


subtracts right operand from the left
-= C -= A is equivalent to C = C - A
operand and assign the result to left
operand

Multiply AND assignment operator, It


multiplies right operand with the left
*= C *= A is equivalent to C = C * A
operand and assign the result to left
operand

Divide AND assignment operator, It


divides left operand with the right
/= C /= A is equivalent to C = C / A
operand and assign the result to left
operand

Modulus AND assignment operator, It


%= takes modulus using two operands and C %= A is equivalent to C = C % A
assign the result to left operand

Example:

<html>
<head><title>Assignment Operators</title><head>
<body>
<?php
$a = 42;
$b = 20;

$c = $a + $b; /* Assignment operator */


echo "Addtion Operation Result: $c <br/>";
$c += $a; /* c value was 42 + 20 = 62 */
echo "Add AND Assigment Operation Result: $c <br/>";
$c -= $a; /* c value was 42 + 20 + 42 = 104 */
echo "Subtract AND Assignment Operation Result: $c <br/>";
$c *= $a; /* c value was 104 - 42 = 62 */
echo "Multiply AND Assignment Operation Result: $c <br/>";
$c /= $a; /* c value was 62 * 42 = 2604 */
echo "Division AND Assignment Operation Result: $c <br/>";
$c %= $a; /* c value was 2604/42 = 62*/
echo "Modulus AND Assignment 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>

Conditional Operator

Operator Description Example


If Condition is true ? Then value X : Otherwise value
?: Conditional Expression
Y

Example:

<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
$a = 10;
$b = 20;

/* If condition is true then assign a to result otheriwse b */


$result = ($a > $b ) ? $a :$b;
echo "TEST1 : Value of result is $result<br/>";
/* If condition is true then assign a to result otheriwse b */
$result = ($a < $b ) ? $a :$b;
echo "TEST2 : Value of result is $result<br/>";
?>
</body>
</html>

Precedence of PHP Operators:

Category Operator Associativity


Unary ! ++ -- Right to left
Multiplicative */% Left to right
Additive +- Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
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]

Assignment = += -= *= /= %= Right to left

MicroDots Computers Education,


103,Royal Buildings, Sathy Road,
Near Bus Stand Roundtana Signal,
Erode. Ph: 2224404 Cell: 97888 73008

You might also like