“php .com” Kode Jawaban

php .com


<p>This is going to be ignored by PHP and displayed by the browser.</p>
<?php echo 'While this is going to be parsed.'; ?>
<p>This will also be ignored by PHP and displayed by the browser.</p>
Dev

php .com

<?php
$a = 1; /* global scope */ 

function test()
{ 
    echo $a; /* reference to local scope variable */ 
} 

test();
?>
Dev

php .com


Regarding earlier note by @purkrt :

> I would like to stress out that the opening tag is "<?php[whitespace]", not just "<?php"

This is absolutely correct, but the wording may confuse some developers less familiar with the extent of the term "[whitespace]".

Whitespace, 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:

<?php/*blah*/ echo "a"?> 

would not work, as mentioned, but :

<?php /*php followed by space*/ echo "a"?>

will work, as well as :

<?php
/*php followed by end-of-line*/ echo "a"?>

and :

<?php    /*php followed by tab*/ echo "a"?>

I 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 :

<?php 
/*php followed by a space and end-of-line*/ echo "a"?>

The end-of-line character is whitespace, so it is all that you would need.
Dev

php .com

<?php
$foo = "1";  // $foo is string (ASCII 49)
$foo *= 2;   // $foo is now an integer (2)
$foo = $foo * 1.3;  // $foo is now a float (2.6)
$foo = 5 * "10 Little Piggies"; // $foo is integer (50)
$foo = 5 * "10 Small Pigs";     // $foo is integer (50)
?>
Dev

php .com


"<script language="php"> </script>, are always available." since PHP 7.0.0 is no longer true. These are removed along the ASP "<%, %>, <%=" tags.
Dev

php .com

<?php
class SimpleClass
{
    // property declaration
    public $var = 'a default value';

    // method declaration
    public function displayVar() {
        echo $this->var;
    }
}
?>
Dev

php .com


One 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:

<?php
  $file_contents  = '<?php die(); ?>' . "\n";
?>

If you try to remove it by turning it into a comment, you get this:

<?php
//  $file_contents  = '<?php die(); ?>' . "\n";
?>

Which results in ' . "\n"; (and whatever is in the lines following it) to be output to your HTML page.

The cure is to either comment it out using /* */ tags, or re-write the line as:

<?php
  $file_contents  = '<' . '?php die(); ?' . '>' . "\n";
?>

Dev

php .com


it's perhaps not obvious to some, but the following code will cause a parse error! the ?> in //?> is not treated as commented text, this is a result of having to handle code on one line such as <?php echo 'something'; //comment ?>

<?php
if(1==1)
{
    //?>
}
?>

i discovered this "anomally" when i commented out a line of code containing a regex which itself contained ?>, with the // style comment.
e.g. //preg_match('/^(?>c|b)at$/', 'cat', $matches);
will cause an error while commented! using /**/ style comments provides a solution. i don't know about # style comments, i don't ever personally use them.
Dev

php .com


Comments in PHP can be used for several purposes, a very interesting one being that you can generate API documentation directly from them by using PHPDocumentor (http://www.phpdoc.org/).

Therefor one has to use a JavaDoc-like comment syntax (conforms to the DocBook DTD), example:
<?php
/**
* The second * here opens the DocBook commentblock, which could later on<br>
* in your development cycle save you a lot of time by preventing you having to rewrite<br>
* major documentation parts to generate some usable form of documentation.
*/
?>
Some basic html-like formatting is supported with this (ie <br> tags) to create something of a layout.
Dev

php .com

<?php
$foo = 'Bob';              // Assign the value 'Bob' to $foo
$bar = &$foo;              // Reference $foo via $bar.
$bar = "My name is $bar";  // Alter $bar...
echo $bar;
echo $foo;                 // $foo is altered too.
?>
Dev

Jawaban yang mirip dengan “php .com”

Pertanyaan yang mirip dengan “php .com”

Lebih banyak jawaban terkait untuk “php .com” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya