PHP/Language Basics/Php Script

Материал из Web эксперт
Перейти к: навигация, поиск

A First PHP Script

   <source lang="html4strict">

<?php

  phpinfo();

?>

 </source>
   
  


A PHP Script Including HTML

   <source lang="html4strict">

<html> <head> <title>A PHP Script Including HTML</title> </head> <body>

<?php print "hello world"; ?>

</body> </html>

 </source>
   
  


Embedding multiple PHP scripts in a single document

   <source lang="html4strict">

<html> <head> <title> <?

   print "PHP-enabled page";
   $variable = "Hello World!";

?> </title></head> <body> <? print $variable; ?> </body> </html>

 </source>
   
  


Hello, World!

   <source lang="html4strict">

<html> <head><title>PHP says hello</title></head> <body> <?php print "Hello, World!"; ?> </body> </html>

 </source>
   
  


HTML and PHP together

   <source lang="html4strict">

<html> <head> <title>HTML and PHP together</title> </head> <body>

<?php print "Welcome"; ?>

</body> </html>

 </source>
   
  


Instruction separation

   <source lang="html4strict">

<?php echo "This is a test"; ?> <?php echo "This is a test" ?>

 </source>
   
  


Keywords and function names are case insensitive

   <source lang="html4strict">

<? // These four lines all do the same thing print number_format(285266237); PRINT Number_Format(285266237); Print number_format(285266237); pRiNt NUMBER_FORMAT(285266237); ?>

 </source>
   
  


Magic Constants

   <source lang="html4strict">

Name Description __FILE__ Name of current file __LINE__ Current line number __FUNCTION__ Name of current function __CLASS__ Name of current class __METHOD__ Name of current method

<?php define("DEBUG", true); function debug_print($var, $file = __FILE__, $line = __LINE__) {

 if (DEBUG) {
 $where = "File = $file ($line)";
   switch (strtolower(substr(php_sapi_name(), 0, 3))) {
     case "cli" :
       echo "$where\n";
       var_dump($var);
       break;
     default :
       echo "$where
";
print("
");
        var_dump($var);
        print("
");
       break;
   }
 }

} ?>

<?php define("DEBUG", true); function debug_print($var) {

 if (DEBUG) {
   switch (strtolower(substr(php_sapi_name(), 0, 3))) {
     case "cli" :
       var_dump($var);
       break;
     default :
print("
");
        var_dump($var);
        print("
");
       break;
   }
 }

} ?>

 </source>
   
  


Mixed-Mode Processing

   <source lang="html4strict">

<?php

           if ($logged_in =  = true) {
                   print "Lots of stuff here";
                   print "Lots of stuff here";
                   print "Lots of stuff here";
                   print "Lots of stuff here";
                   print "Lots of stuff here";
           }
   ?>


   <?php
           if ($logged_in =  = true) {
   ?>
           Lots of stuff here
           Lots of stuff here
           Lots of stuff here
           Lots of stuff here
           Lots of stuff here
   <?php
           }
   ?>
 
 </source>
   
  


Multiple start and end tags

   <source lang="html4strict">

Five plus five is: <?php print 5 + 5; ?>

Four plus four is: <?php print 4 + 4; ?> <p> <img src="vacation.jpg" alt="My Vacation"> </source>

Opening and Closing Code Islands

   <source lang="html4strict">

<? "Hello, world!" ?>

Here is the equivalent, written using the standard open and closing tags:

   <?php
           print "Hello, world!";
   ?>
 
 </source>
   
  


PHP 5 Data Types

   <source lang="html4strict">

Data Type Description Boolean Stores either a true or false value Integer Stores a numeric value that is a whole number Double Stores a numeric value that can contain a number of decimal places (commonly called a float) String Stores a chain of characters Array Stores an indexed container of values Object Stores an instance of a defined class Resource Holds a reference to an external source NULL Represents a variable that has no value

 </source>
   
  


PHP supports trigonometric and logarithmic operations

   <source lang="html4strict">

<?php

   $cos = cos(2 * M_PI);    /* cos of 2*PI is 1 */

?>

 </source>
   
  


Spaces, tabs, and blank lines in between statements have no effect

   <source lang="html4strict">

<?php

           $name = "Paul"; print "Your name is $name\n";
           $name2 = $name; $age = 20;
           print "Your name is $name2, and your age is $age\n";
           print "Goodbye, $name!\n";
   ?>
 
 </source>
   
  


Spacing

   <source lang="html4strict">

<?php

   print        "test"           ;
   print"test";
   print "test";

?>

</source>