PHP:Guide/Grunnleggende

Fra CodeWiki

Gå til: navigasjon, søk

Tilbake til Guide.

Innhold

Eksemplene

Eksempel 1 - Generelle aritmetiske operatorer

Dette eksempelet bruker de fem generelle operatorene brukt i matematiske uttrykk. Disse er grunnlaget for alle matematiske og string operasjoner i PHP.

De fem aritmetiske operatorene er all identiske med de i C++ og Java:

  • addere (+)
  • subtrahere (-)
  • multiplisere (*)
  • dividere (/)
  • tildeling (=)


Studer dette eksempelet. Hvert matematisk uttrykk til høyre for tildelings-operatoren blir evaluert og den vanlige rekkefølgen blir brukt (gange/dele, så legge sammen/trekke fra). Når uttrykket har blitt evaluert, blir resultatet tildelt variabelen til venstre for tildelings-operatoren.

PHP Kode:

<?php
  $x = 25; // 25
  $y = 10; // 10
  $z = $x + $y; // 35
  echo $z; // 35
 
  echo "<br />"; // <br />
  $z = $x / $y; // 2.5
  echo $z; // 2.5
 
  echo "<br />"; // <br />
  $z = $y * $y * $x; // 2500
  echo $z - 1250; // 1250
  echo "<br />"; // <br />
?>

PHP Utdata:

35<br />2.5<br />1250<br />

HTML:

35
2.5
1250


Notat: Hvis du ikke er kjent med (X)HTML, vet du kanskje ikke meningen med denne delen av koden:
echo "<br />";
Meningen er å sette inn en ny linje mellom resultatene. Med andre ord gjør det at hvert resultat kommer på en ny linje. Uten denne linjen ville koden over gitt utdataen:
352.51250

Dette er selvsagt ikke det resultatet vi ville ha


There are two code options which perform the opposite of the assign (=) operator. The keyword null should be used for variable nullification, which is actually used with the assign (=) operator in place of a value. If you want to destroy a variable, the unset() language construct is available.


Examples:
$variable = null;

eller

unset($variable);


Eksempel 2 - String concatenation

This example demonstrates the concatenation operator (.), which joins together two strings, producing one string consisting of both parts. It is analogous to the plus (+) operator commonly found in C++ string class (see STL), Python, Java, JavaScript implementations.

The statement
$string = $string . " All the cool kids are doing it.";
prepends the current value of $string (which is "PHP is wonderful and great.") to the literal string " All the cool kids are doing it." and assigns this new string to $string.


Kode:

<?php
  $string = "PHP is wonderful and great.";
  $string = $string . " All the cool kids are doing it.";
  echo $string;
?>

Utdata:

PHP is wonderful and great. All the cool kids are doing it.


Eksempel 3 - Shortcut operators

This snippet demonstrates self-referential shortcut operators. The first such operator is the ++ operator, which increments $x (using the postfix form) by 1 giving it the value 2. After incrementing $x, $y is defined and assigned the value 5.

The second shortcut operator is *=, which takes $y and assigns it the value $y * $x, or 10.

After initializing $z to 180, the subsequent line performs two shortcut operations. Going by order of operations (see manual page below), $y is decremented (using the prefix form) and divided into $z. $z is assigned to the resulting value, 20.


Kode:

<?php
  $x = 1;
  $x++;
  echo $x . " ";
  $y = 5;
  $y *= $x;
  echo $y . " ";
  $z = 180;
  $z /= --$y;
  echo $z;
?>

Utdata:

2 10 20


Note: The expanded version of the above code (without the shortcut operators) looks like this:
<?php
  $x = 1;
  $x = $x + 1;
  echo $x . " ";
  $y = 5;
  $y = $y * $x;
  echo $y . " ";
  $z = 180;
  $y = $y - 1;
  $z = $z / $y;
  echo $z;
?>
The output is the same as seen in the above example.


New Concepts

Operators

An operator is any symbol used in an expression used to manipulate data. The seven basic PHP operators are:

  • = (assignment)
  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  •  % (modulus)
  • . (concatenation)

In addition, each of the above operators can be combined with an assignment operation, creating the operators below:

  • += (addition assignment)
  • -= (subtraction assignment)
  • *= (multiplication assignment)
  • /= (division assignment)
  •  %= (modulus assignment)
  • .= (concatenation assignment)

These operators are used when a variable is added, subtracted, multiplied or divided by a second value and subsequently assigned to itself.

In other words, the statements
$var = $var + 5;

and

$var += 5;
are equivalent.


There are also increment and decrement operators in PHP.

  • ++ (increment)
  • -- (decrement)

These are a special case of the addition and subtraction assignment operators.

This code uses the addition assignment operator to increment and decrement a variable.

Kode:

$var = 0;

$var += 1;
echo "The incremented value is $var.\n";

$var -= 1;
echo "The decremented value is $var.\n";

Utdata:

The incremented value is 1.
The decremented value is 0.

While this is perfectly legal in PHP, it is somewhat lengthy for an operation as common as this. It can easily be replaced by the increment operator, shortening the statement.

This code snippet uses the increment and decrement operators to increase and decrease a variable's value by one.

Kode:

$var = 3;

$var++;
echo "The incremented value is $var.\n";

$var--;
echo "The decremented value is $var.\n";

Utdata:

The incremented value is 4.
The decremented value is 3.
Using the increment operator makes your code slightly easier to read and understand.


For a more in-depth overview of PHP's operators, including an explanation of bitwise operators, refer to the manual link below.

Newline and Other Special Characters

Both of the below examples make use of the newline character (\n) to signify the end of the current line and the beginning of a new one.


The newline is used as follows:

Kode:

echo "PHP is cool,\nawesome,\nand great.";

Utdata:

PHP is cool,
awesome,
and great.


Notice: the line break occurs in the output where ever the \n occurs in the string in the echo statement. However, a \n will not produce a newline when the HTML document is displayed in a web browser. This is because the PHP engine does not render the script. Instead, the PHP engine outputs HTML code, which is subsequently rendered by the web browser. The linebreak \n in the PHP script becomes HTML whitespace, which is skipped when the web browser renders it (much like the whitespace in a PHP script is skipped when the PHP engine generates HTML). This does not mean that the \n operator is useless; it can be used to add whitespace to your HTML, so if someone views the HTML generated by your PHP script they'll have an easier time reading it.

In order to insert a line-break that will be rendered by a web browser, you must instead use the <br /> tag to break a line.


Therefore the statement above would be altered like so:
echo 'PHP is cool,<br />awesome<br />and great.';


The function nl2br() is available to automatically convert newlines in a string to <br /> tags.


The string must be passed through the function, and then reassigned:

PHP Kode:

$string = "This\ntext\nbreaks\nlines.";
$string = nl2br($string);
print $string;

PHP Utdata:

This<br />
text<br />
breaks<br />
lines.

HTML:

This
text
breaks
lines.
Additionally, the PHP output (HTML source code) generated by the above example includes linebreaks.


Other special characters include the ASCII NUL (\0) - used for padding binary files, tab (\t) - used to display a standard tab, and return (\r) - signifying a carriage return. Again, these characters do not change the rendering of your HTML since they add whitespace to the HTML source. In order to have tabs and carriage returns rendered in the final web page, &tab; should be used for tabs and <br /> should be used for a carriage return.

For More Information

Personlige verktøy
dataprogrammering
generelt