Beruflich Dokumente
Kultur Dokumente
Arithmetic operators
Increment/Decrement operators - Subtraction
Logical operators
** Exponentiation
PHP Assignment Operators Assignment Same Description
as...
The PHP assignment operators are used with
numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It x=y x=y The left operand gets set to the
means that the left operand gets set to the value value of the expression on the
right
of the assignment expression on the right.
x += y x=x Addition
+y
x -= y x=x- Subtraction
y
x *= y x=x* Multiplication
y
x /= y x=x/ Division
y
x %= y x=x Modulus
%y
PHP Logical Operators
The PHP logical operators are used to combine
conditional statements.
=== Identity $x === Returns true if $x and $y have the same key/value pairs in the
$y same order and of the same types
<?php <?php
$t = date("H"); define("GREETING", "Welcome to
W3Schools.com!");
if ($t < "20") { echo GREETING;
echo "Have a good day!"; ?>
}
?> Create a constant with a case-in
sensitive name:
PHP Constants <?php
A constant is an identifier (name) for a simple
define("GREETING", "Welcome to
value. The value cannot be changed during the W3Schools.com!", true);
script. echo greeting;
?>
A valid constant name starts with a letter or
underscore (no $ sign before the constant
name).
The PHP switch Statement <?php
$favcolor = "red";
Use the switch statement to select one of
many blocks of code to be executed. switch ($favcolor) {
case "red":
Syntax echo "Your favorite color is
red!";
switch (n) { break;
case label1: case "blue":
code to be executed if echo "Your favorite color is
n=label1; blue!";
break; break;
case label2: case "green":
code to be executed if echo "Your favorite color is
n=label2; green!";
break;
break;
default:
case label3: echo "Your favorite color is
code to be executed if neither red, blue, nor green!";
n=label3; }
break; ?>
...
default:
code to be executed if n is
different from all labels;
}
PHP Loops for - loops through a block of code a
specified number of times
Often when you write code, you want the same for (init counter; test counter;
block of code to run over and over again a certain increment counter) {
number of times. So, instead of adding several code to be executed for each
almost equal code-lines in a script, we can use
iteration;
loops.
}
Loops are used to execute the same block of
code again and again, as long as a certain foreach - loops through a block of code for
condition is true. each element in an array
do {
code to be executed;
} while (condition is true);
PHP String Functions
The PHP string functions are part of the PHP
core. No installation is required to use these
functions.
fmod()
Returns the remainder of x/y
PHP Variables
Rules for PHP variables:
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
PHP Arrays, foreach
command and Array
Functions
A.
array_shift() Removes the first element from an array, and returns the value
of the removed element
array_search() Searches an array for a given value and returns the key
The example below shows how to use some of The example below shows a form with an input
the elements in $_SERVER: field and a submit button. When a user submits
the data by clicking on "Submit", the form data
<?php is sent to the file specified in the action attribute
echo $_SERVER['PHP_SELF']; of the <form> tag. In this example, we point to
echo "<br>"; this file itself for processing form data. If you
wish to use another PHP file to process form
echo $_SERVER['SERVER_NAME'];
data, replace that with the filename of your
echo "<br>"; choice. Then, we can use the super global
echo $_SERVER['HTTP_HOST']; variable $_REQUEST to collect the value of the
echo "<br>"; input field:
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
<html>
<body>
PHP $_POST
PHP $_POST is a PHP super global variable
<form method="post" action="<?php echo which is used to collect form data after
$_SERVER['PHP_SELF'];?>"> submitting an HTML form with method="post".
Name: <input type="text" name="fname $_POST is also widely used to pass variables.
"> The example below shows a form with an input
<input type="submit"> field and a submit button. When a user submits
</form> the data by clicking on "Submit", the form data
is sent to the file specified in the action
<?php attribute of the <form> tag. In this example,
we point to the file itself for processing form
if ($_SERVER["REQUEST_METHOD"] data. If you wish to use another PHP file to
== "POST") { process form data, replace that with the
// collect value of input field filename of your choice. Then, we can use the
$name = $_REQUEST['fname']; super global variable $_POST to collect the
if (empty($name)) { value of the input field:
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
<html>
<body>
PHP $_GET
PHP $_GET is a PHP super global variable which
<form method="post" action="<?php echo is used to collect form data after submitting an
$_SERVER['PHP_SELF'];?>"> HTML form with method="get".
Name: <input type="text" name="fname
$_GET can also collect data sent in the URL.
">
<input type="submit"> Assume we have an HTML page that contains a
</form> hyperlink with parameters:
<html>
<?php <body>
if ($_SERVER["REQUEST_METHOD"]
== "POST") { <a
// collect value of input field href="test_get.php?subject=PHP&web=W3schools
$name = $_POST['fname']; .com">Test $GET</a>
if (empty($name)) {
</body>
echo "Name is empty"; </html>
} else {
echo $name; When a user clicks on the link "Test $GET", the
} parameters "subject" and "web" are sent to
} "test_get.php", and you can then access their
values in "test_get.php" with $_GET.
?>
The example below shows the code in
</body> "test_get.php":
</html>
PHP $_COOKIES
Cookies are small text files loaded from a server to a
<html>
client computer storing some information regarding the
<body> client computer, so that when the same page from the
server is visited by the user, necessary information can
<?php be collected from the cookie itself, decreasing the
latency to open the page.
echo "Study " . $_GET['subject'] . "
at " . $_GET['web'];
?>
PHP $_SESSION
Sessions are wonderful ways to pass variables. All you need
</body> to do is start a session by session_start();Then all the
variables you store within a $_SESSION, you can access it
</html> from anywhere on the server. Here is an example:
PHP $_FILES
$_FILES is a super global variable which can be used
to upload files. Here we will see an example in which
our php script checks if the form to upload the file is
being submitted and generates a message if true.
PHP $_ENV
$_ENV is used to return the environment variables from
the web server.