1
2"<script language="php"> </script>, are always available." since PHP 7.0.0 is no longer true. These are removed along the ASP "<%, %>, <%=" tags.
3
1<?php
2class A
3{
4 function foo()
5 {
6 if (isset($this)) {
7 echo '$this is defined (';
8 echo get_class($this);
9 echo ")\n";
10 } else {
11 echo "\$this is not defined.\n";
12 }
13 }
14}
15
16class B
17{
18 function bar()
19 {
20 A::foo();
21 }
22}
23
24$a = new A();
25$a->foo();
26
27A::foo();
28
29$b = new B();
30$b->bar();
31
32B::bar();
33?>
1
2Regarding earlier note by @purkrt :
3
4> I would like to stress out that the opening tag is "<?php[whitespace]", not just "<?php"
5
6This is absolutely correct, but the wording may confuse some developers less familiar with the extent of the term "[whitespace]".
7
8Whitespace, in this context, would be any character that generated vertical or horizontal space, including tabs ( \t ), newlines ( \n ), and carriage returns ( \r ), as well as a space character ( \s ). So reusing purkrt's example:
9
10<?php/*blah*/ echo "a"?>
11
12would not work, as mentioned, but :
13
14<?php /*php followed by space*/ echo "a"?>
15
16will work, as well as :
17
18<?php
19/*php followed by end-of-line*/ echo "a"?>
20
21and :
22
23<?php /*php followed by tab*/ echo "a"?>
24
25I just wanted to clarify this to prevent anyone from misreading purkrt's note to mean that a the opening tag --even when being on its own line--required a space ( \s ) character. The following would work but is not at all necessary or how the earlier comment should be interpreted :
26
27<?php
28/*php followed by a space and end-of-line*/ echo "a"?>
29
30The end-of-line character is whitespace, so it is all that you would need.
31
1
2<p>This is going to be ignored by PHP and displayed by the browser.</p>
3<?php echo 'While this is going to be parsed.'; ?>
4<p>This will also be ignored by PHP and displayed by the browser.</p>
5
1<?php
2class SimpleClass
3{
4 // property declaration
5 public $var = 'a default value';
6
7 // method declaration
8 public function displayVar() {
9 echo $this->var;
10 }
11}
12?>
1<?php
2$foo = "1"; // $foo is string (ASCII 49)
3$foo *= 2; // $foo is now an integer (2)
4$foo = $foo * 1.3; // $foo is now a float (2.6)
5$foo = 5 * "10 Little Piggies"; // $foo is integer (50)
6$foo = 5 * "10 Small Pigs"; // $foo is integer (50)
7?>
1<?php
2
3// Pre PHP 7 code
4class Logger
5{
6 public function log($msg)
7 {
8 echo $msg;
9 }
10}
11
12$util->setLogger(new Logger());
13
14// PHP 7+ code
15$util->setLogger(new class {
16 public function log($msg)
17 {
18 echo $msg;
19 }
20});
1<?php
2$a_bool = TRUE; // a boolean
3$a_str = "foo"; // a string
4$a_str2 = 'foo'; // a string
5$an_int = 12; // an integer
6
7echo gettype($a_bool); // prints out: boolean
8echo gettype($a_str); // prints out: string
9
10// If this is an integer, increment it by four
11if (is_int($an_int)) {
12 $an_int += 4;
13}
14
15// If $a_bool is a string, print it out
16// (does not print out anything)
17if (is_string($a_bool)) {
18 echo "String: $a_bool";
19}
20?>
1
2One aspect of PHP that you need to be careful of, is that ?> will drop you out of PHP code and into HTML even if it appears inside a // comment. (This does not apply to /* */ comments.) This can lead to unexpected results. For example, take this line:
3
4<?php
5 $file_contents = '<?php die(); ?>' . "\n";
6?>
7
8If you try to remove it by turning it into a comment, you get this:
9
10<?php
11// $file_contents = '<?php die(); ?>' . "\n";
12?>
13
14Which results in ' . "\n"; (and whatever is in the lines following it) to be output to your HTML page.
15
16The cure is to either comment it out using /* */ tags, or re-write the line as:
17
18<?php
19 $file_contents = '<' . '?php die(); ?' . '>' . "\n";
20?>
21
22
1<?php
2/**
3 * Define MyClass
4 */
5class MyClass
6{
7 public $public = 'Public';
8 protected $protected = 'Protected';
9 private $private = 'Private';
10
11 function printHello()
12 {
13 echo $this->public;
14 echo $this->protected;
15 echo $this->private;
16 }
17}
18
19$obj = new MyClass();
20echo $obj->public; // Works
21echo $obj->protected; // Fatal Error
22echo $obj->private; // Fatal Error
23$obj->printHello(); // Shows Public, Protected and Private
24
25
26/**
27 * Define MyClass2
28 */
29class MyClass2 extends MyClass
30{
31 // We can redeclare the public and protected properties, but not private
32 public $public = 'Public2';
33 protected $protected = 'Protected2';
34
35 function printHello()
36 {
37 echo $this->public;
38 echo $this->protected;
39 echo $this->private;
40 }
41}
42
43$obj2 = new MyClass2();
44echo $obj2->public; // Works
45echo $obj2->protected; // Fatal Error
46echo $obj2->private; // Undefined
47$obj2->printHello(); // Shows Public2, Protected2, Undefined
48
49?>