mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Merged emarref's changes
This commit is contained in:
commit
dd3b8b3436
@ -87,7 +87,7 @@ $sgl_quotes
|
|||||||
END; // Nowdoc syntax is available in PHP 5.3.0
|
END; // Nowdoc syntax is available in PHP 5.3.0
|
||||||
|
|
||||||
// Manipulation
|
// Manipulation
|
||||||
$concatenated = $sgl_quotes + $dbl_quotes;
|
$concatenated = $sgl_quotes . $dbl_quotes;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Compound
|
### Compound
|
||||||
@ -119,6 +119,8 @@ print('Hello World!'); // The same as echo
|
|||||||
|
|
||||||
// echo is actually a language construct, so you can drop the parentheses.
|
// echo is actually a language construct, so you can drop the parentheses.
|
||||||
echo 'Hello World!';
|
echo 'Hello World!';
|
||||||
|
print 'Hello World!'; // So is print
|
||||||
|
|
||||||
echo 100;
|
echo 100;
|
||||||
echo $variable;
|
echo $variable;
|
||||||
echo function_result();
|
echo function_result();
|
||||||
@ -135,12 +137,12 @@ echo function_result();
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
$a = 1;
|
$x = 1;
|
||||||
$b = 2;
|
$y = 2;
|
||||||
$a = $b; // A now contains the same value sa $b
|
$x = $y; // A now contains the same value sa $y
|
||||||
$a =& $b;
|
$x = &$y;
|
||||||
// A now contains a reference to $b. Changing the value of
|
// $x now contains a reference to $y. Changing the value of
|
||||||
// $a will change the value of $b also, and vice-versa.
|
// $x will change the value of $y also, and vice-versa.
|
||||||
```
|
```
|
||||||
|
|
||||||
### Comparison
|
### Comparison
|
||||||
@ -148,15 +150,20 @@ $a =& $b;
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
// These comparisons will always be true, even if the types aren't the same.
|
||||||
$a == $b // TRUE if $a is equal to $b after type juggling.
|
$a == $b // TRUE if $a is equal to $b after type juggling.
|
||||||
$a === $b // TRUE if $a is equal to $b, and they are of the same type.
|
|
||||||
$a != $b // TRUE if $a is not equal to $b after type juggling.
|
$a != $b // TRUE if $a is not equal to $b after type juggling.
|
||||||
$a <> $b // TRUE if $a is not equal to $b after type juggling.
|
$a <> $b // TRUE if $a is not equal to $b after type juggling.
|
||||||
$a !== $b // TRUE if $a is not equal to $b, or they are not of the same type.
|
|
||||||
$a < $b // TRUE if $a is strictly less than $b.
|
$a < $b // TRUE if $a is strictly less than $b.
|
||||||
$a > $b // TRUE if $a is strictly greater than $b.
|
$a > $b // TRUE if $a is strictly greater than $b.
|
||||||
$a <= $b // TRUE if $a is less than or equal to $b.
|
$a <= $b // TRUE if $a is less than or equal to $b.
|
||||||
$a >= $b // TRUE if $a is greater than or equal to $b.
|
$a >= $b // TRUE if $a is greater than or equal to $b.
|
||||||
|
|
||||||
|
// The following will only be true the values match and they are the same type.
|
||||||
|
$a === $b // TRUE if $a is equal to $b, and they are of the same type.
|
||||||
|
$a !== $b // TRUE if $a is not equal to $b, or they are not of the same type.
|
||||||
|
1 == '1' // TRUE
|
||||||
|
1 === '1' // FALSE
|
||||||
```
|
```
|
||||||
|
|
||||||
## [Type Juggling](http://www.php.net/manual/en/language.types.type-juggling.php)
|
## [Type Juggling](http://www.php.net/manual/en/language.types.type-juggling.php)
|
||||||
@ -176,7 +183,11 @@ echo $string + $string;
|
|||||||
$string = 'one';
|
$string = 'one';
|
||||||
echo $string + $string;
|
echo $string + $string;
|
||||||
// Outputs 0 because the + operator cannot cast the string 'one' to a number
|
// Outputs 0 because the + operator cannot cast the string 'one' to a number
|
||||||
|
```
|
||||||
|
|
||||||
|
Type casting can be used to treat a variable as another type temporarily by using cast operators in parentheses.
|
||||||
|
|
||||||
|
```php
|
||||||
$boolean = (boolean) $integer; // $boolean is true
|
$boolean = (boolean) $integer; // $boolean is true
|
||||||
|
|
||||||
$zero = 0;
|
$zero = 0;
|
||||||
@ -222,9 +233,9 @@ if (/* test */) {
|
|||||||
?>
|
?>
|
||||||
|
|
||||||
<?php if (/* test */): ?>
|
<?php if (/* test */): ?>
|
||||||
<!-- Do something that isn't PHP -->
|
This is displayed if the test is truthy.
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<!-- Do something default -->
|
This is displayed otherwise.
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -278,7 +289,6 @@ while ($i < 5) {
|
|||||||
if ($i == 3) {
|
if ($i == 3) {
|
||||||
break; // Exit out of the while loop and continue.
|
break; // Exit out of the while loop and continue.
|
||||||
}
|
}
|
||||||
|
|
||||||
echo $i++;
|
echo $i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -288,7 +298,6 @@ while ($i < 5) {
|
|||||||
if ($i == 3) {
|
if ($i == 3) {
|
||||||
continue; // Skip this iteration of the loop
|
continue; // Skip this iteration of the loop
|
||||||
}
|
}
|
||||||
|
|
||||||
echo $i++;
|
echo $i++;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -338,6 +347,23 @@ function outer_function ($arg_1 = null) { // $arg_1 is optional
|
|||||||
// inner_function() does not exist and cannot be called until outer_function() is called
|
// inner_function() does not exist and cannot be called until outer_function() is called
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This enables [currying](http://en.wikipedia.org/wiki/Currying) in PHP.
|
||||||
|
|
||||||
|
```php
|
||||||
|
function foo ($x, $y, $z) {
|
||||||
|
echo "$x - $y - $z";
|
||||||
|
}
|
||||||
|
|
||||||
|
function bar ($x, $y) {
|
||||||
|
return function ($z) use ($x, $y) {
|
||||||
|
foo($x, $y, $z);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
$bar = bar('A', 'B');
|
||||||
|
$bar('C');
|
||||||
|
```
|
||||||
|
|
||||||
### [Variable](http://www.php.net/manual/en/functions.variable-functions.php)
|
### [Variable](http://www.php.net/manual/en/functions.variable-functions.php)
|
||||||
|
|
||||||
```php
|
```php
|
||||||
@ -355,7 +381,11 @@ Similar to variable functions, functions may be anonymous.
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
my_function(function () {
|
function my_function($callback) {
|
||||||
|
$callback('My argument');
|
||||||
|
}
|
||||||
|
|
||||||
|
my_function(function ($my_argument) {
|
||||||
// do something
|
// do something
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -396,12 +426,10 @@ class MyClass {
|
|||||||
function myFunction() {
|
function myFunction() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function function youCannotOverrideMe()
|
final function youCannotOverrideMe() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function myStaticMethod()
|
public static function myStaticMethod() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -438,7 +466,8 @@ echo $x->property; // Will use the __get() method
|
|||||||
$x->property = 'Something'; // Will use the __set() method
|
$x->property = 'Something'; // Will use the __set() method
|
||||||
```
|
```
|
||||||
|
|
||||||
Classes can be abstract (using the ```abstract``` keyword), extend other classes (using the ```extends``` keyword) and implement interfaces (using the ```implements``` keyword). An interface is declared with the ```interface``` keyword.
|
Classes can be abstract (using the ```abstract``` keyword), extend other classes (using the ```extends``` keyword) and
|
||||||
|
implement interfaces (using the ```implements``` keyword). An interface is declared with the ```interface``` keyword.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
Loading…
Reference in New Issue
Block a user