Sie sind auf Seite 1von 7

Static:

A website is collection of documents written in the HTML language. When a user looks
at a website with a browser (e.g. Netscape), the browser is able to follow the instructions
presented to it in HTML to make a website look a certain way.
<html>
<title>An Average Website</title>
<body bgcolor="#003399" text="#ffcc33">
<h1>An Average Website</h1>
<p>This is an average website.
</html>

hat is, if the user were to reload a static website, they would see the exact same content
every time. Its content was written directly by an author, and when the user goes to the
site, that code is downloaded into a browser and interpreted.
In contrast to a static website, a dynamic website is one whose content is regenerated
every time a user visits or reloads the site. If you click on the "Reload" button several
times, you should notice that the time will change.
<html>
<h3>The Date & Time: </h3>
<? echo (date ("l dS of F Y h:i:s A")); ?>
</html>

PHP: Hypertext Preprocessor, an open source, server-side, HTML embedded scripting


language used to create dynamic Web pages.
Syntax:
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP scripting code.

we have an example of a simple PHP file,


to output the text "Hello World!" on a web page:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Output:

My first PHP page


Hello World!

PHP Case Sensitivity


In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.)
are NOT case-sensitive.
However; in PHP, all variables are case-sensitive.

Variables
Variables are "containers" for storing information:
Rules for PHP variables:

A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _ )
Variable names are case sensitive ($y and $Y are two different variables)

Creating (Declaring) PHP Variables


PHP has no command for declaring a variable.
<?php
$txt="Hello world!";
$x=5;
$y=10.5;
echo $y;
?>

PHP is a Loosely Typed Language


In the example above, notice that we did not have to tell PHP which data type the
variable is.
PHP automatically converts the variable to the correct data type, depending on its value.

The PHP print Statement


print is also a language construct, and can be used with or without parentheses: print or
print().
<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

PHP is fun!
Hello world!
I'm about to learn PHP!

Data Types
String, Integer, Floating point numbers, Boolean, Array, Object, NULL.
<?php
$x = "Hello world!";
echo $x;
$x = 5985;
echo $x;
$x = 10.365;
echo $x;

$x=true;
echo $x;
$cars=array("Volvo","BMW","Toyota");
echo $cars;

PHP String Functions


In this chapter we will look at some commonly used functions to manipulate strings.

The PHP strlen() function


The strlen() function returns the length of a string, in characters.
The example below returns the length of the string "Hello world!":
<?php
echo strlen("Hello world!");
?>
The output of the code above will be: 12

The PHP strpos() function


The strpos() function is used to search for a specified character or text within a string.
If a match is found, it will return the character position of the first match. If no match is
found, it will return FALSE.
The example below searches for the text "world" in the string "Hello world!":

Example
<?php
echo strpos("Hello world!","world");
?>

PHP Arithmetic Operators


Operator
+
*
/

Name
Addition
Subtraction
Multiplication
Division

Example
$x + $y
$x - $y
$x * $y
$x / $y

Result
Sum of $x and $y
Difference of $x and $y
Product of $x and $y
Quotient of $x and $y

Modulus

$x % $y

Remainder of $x divided by $y

PHP Assignment Operators


The PHP assignment operators is used to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the
value of the assignment expression on the right.
Assignment

Same as...

x=y

x=y

x += y
x -= y
x *= y
x /= y
x %= y

x=x+y
x=x-y
x=x*y
x=x/y
x=x%y

Description
The left operand gets set to the value of the expression on
the right
Addition
Subtraction
Multiplication
Division
Modulus

PHP String Operators


Operator

Name

Concatenation

.=

Concatenation
assignment

Example
$txt1 = "Hello"
$txt2 = $txt1 . "
world!"
$txt1 = "Hello"
$txt1 .= " world!"

Result
Now $txt2 contains "Hello
world!"
Now $txt1 contains "Hello
world!"

PHP Increment / Decrement Operators


Operator
++$x
$x++
--$x
$x--

Name
Description
Pre-increment Increments $x by one, then returns $x
Post-increment Returns $x, then increments $x by one
Pre-decrement Decrements $x by one, then returns $x
Post-decrement Returns $x, then decrements $x by one

PHP Comparison Operators


The PHP comparison operators are used to compare two values (number or string):
Operator
Name
==
Equal

Example
$x == $y

===

Identical

$x === $y

!=
<>

Not equal
Not equal

$x != $y
$x <> $y

Result
True if $x is equal to $y
True if $x is equal to $y, and
they are of the same type
True if $x is not equal to $y
True if $x is not equal to $y

!==

Not identical

>
<

Greater than
$x > $y
Less than
$x < $y
Greater than or equal
$x >= $y
to

>=
<=

$x !== $y

Less than or equal to $x <= $y

True if $x is not equal to $y, or


they are not of the same type
True if $x is greater than $y
True if $x is less than $y
True if $x is greater than or
equal to $y
True if $x is less than or equal
to $y

PHP Logical Operators


Operator
and
And
or
Or

Name

Example
$x and $y
$x or $y

xor

Xor

$x xor $y

&&
||
!

And
Or
Not

$x && $y
$x || $y
!$x

Result
True if both $x and $y are true
True if either $x or $y is true
True if either $x or $y is true,
but not both
True if both $x and $y are true
True if either $x or $y is true
True if $x is not true

PHP - The if Statement


The if statement is used to execute some code only if a specified condition is true.

Syntax
if (condition) {
code to be executed if condition is true;
}

PHP - The if...else Statement


Use the if....else statement to execute some code if a condition is true and another code
if the condition is false.

PHP - The if...else Statement


Use the if....else statement to execute some code if a condition is true and another code
if the condition is false.

The PHP switch Statement


Use the switch statement to select one of many blocks of code to be executed.

Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}

The PHP while Loop


The while loop executes a block of code as long as the specified condition is true.

Syntax
while (condition is true) {
code to be executed;
}

The PHP do...while Loop


The do...while loop will always execute the block of code once, it will then check the
condition, and repeat the loop while the specified condition is true.

Syntax
do {
code to be executed;
} while (condition is true);

Das könnte Ihnen auch gefallen