PHP demo1

<!--Content

  1. php comment tags
  2. ways to print a single/double quote to be literally
  3. the difference between print and echo
  4. difference between single quote and double quote
  5. function
  6. here document
  7. <pre> tag
  8. boolean literals
  9. the gettype() function
  10. references (&)
  11. variable variables (dynamic variables)
  12. isset() function
  13. is_null() function
  14. empty() function
  15. unset() function

-->
<!--really important thing need to remember:
Always use double quotes for varibales that contain whitespace:
e.g echo "$var"...-->
<?php
//php tags includes:
/*
1)<?php code ?>
2)<script language="php">code</script>
3)<% code %>
4)<? code ?> shortcut tag
*/

//ways to print a single quote to be literally
print "-ways to print a single quote to be literally:<br />";
print "'";//just put ' inside of double quote(""),it will print the ' out
print "<br />";
print "\'";//wrong.it will print \' for printing a single quote, dont need backslash
print "<br />";

//ways to print a double quote(") to be literally
print "-ways to print a double quote(\") to be literally:<br />";
print "1st''";// the output will be "
print "<br />";
print "2nd\""; // the output will be "
print "<br />";
print '\"'; // the output will be \"
print "<br />";
print '3rd"'; // the last way to print a double quotation mark
print "<br />";


//the difference between print and echo
print "-the difference between print and echo:<br />";
echo "echo use ","' ","to concatenate the strings into one string"," and echo will not return a value"," .echo is slightly faster than print<br />";
print "print use"." ."." to concatenate strings into one string"." .print returns \"1\", so it can be used in an expression. \nfor example:\n\t<br />";
$expression = 0;
if($expression == 0 && print "something" == 1){
echo "print returned \"1\"<br />";
}
$name = name;$state = state; $salary = salary;
//echo( ) – allows multiple comma-separated arguments
echo $name, $state, $salary;
echo $name . $state . $salary;
echo "$name $state $salary<br />";

print $name;
print $name . $state . $salary;
print "$name $state $salary<br />";


//difference between single quote and double quote
print "-difference between single quote and double quote:<br />";
$money = 5;
print "<br/>\$$money";
print '<br/>\$$money';



/*PHP and HTML are different languages
HTML is a markup language, which combines text with tags to define the structure and discribe the way the document will displayed
PHP is a programming language. it consists data, instructions and procedures that tell the computers what operations to perform on the computer.
*/


//There are 3 ways to make comments in php:
//This is the first way
#This is the second way
/*And this is the last way*/

//function name is not case sensitive in php.Variable nemes are case sensitive.
function yoo(){
echo "This is a function<br />";
}
Yoo();
//declare multiple variables:
$first_name;$middle_name;$last_name;

//Here Document
//There are 3 way to use heredoc
print "-3 ways to use heredoc:<br />";
$var = "this string is gonna be used in the Here Docment below";
//1st, print a long sequence of HTML text in a PHP script
print <<<WHATEVER
<p>
<ol>
<li>Use-defined dilimiter word (The name of the Here Document Delimiter must start with a letter) starts and terminate the here document.</li><br />
<li>delimiter (here is "WHATEVER") is preceded by three<</li><br />
<li>Delimiter can surrounded by any spaces, comments or other text.</li><br />
<li>Finial delimiter can optionally be ended with semicolon and must be on a line by itself</li><br />
<li>all variables ($var) and escape sequence (for example \\n \n and \\t \t) are interpreted.</li><br />
</ol>
</p>
WHATEVER;

//2nd,printing a long sequence of HTML text in a PHP function
function print_footer(){
print <<< FOOBAR
<div id ="footer">
printed by Jordan Cheng,<br />
</div>
FOOBAR;
}

print_footer();


//3rd, assign a long sequence of text to a PHP variable
$var = <<< SOMETHING
this is a string inside of \$var.
SOMETHING;
print $var;

 

//<pre> tag
//to display escape sequences in the browser HTML <pre> tag can be used
print "-pre tag:<br />";
echo "<pre>
Text in a pre element
is displayed in a fixed-width
font, and it preserves
both spaces and
line breaks<br />
</pre>";

echo "<pre>
\'->'<br />
\\\"->\"<br />
\\t->\t<br />
\\n->\n<br />
\\\$->\$<br />
\\->\\<br />
</pre>";

 

//Boolean Literals
echo "Boolean Literals:<br />";
echo "<p>logical values that have only one of two values, true or false, both case <u>insensitive</u><br />
When using numeric comparison and equality operators, the value true evaluates to 1 and false evaluates to an empty string<br />
true = 1;
false =\"\";
</p>";


//The gettype() function
/*the argument can be a variable, string, keyword.
can be used to check whether or not a variable has been defined.
if there is no value is associated with the variable, gettype() function return NULL;
*/
echo "-gettype() function<br />";
$null;
print "<br/>".gettype("bulahbulabulah")."<br/>";
print gettype(123)."<br/>";
print gettype($null)."<br/>";


//references
echo "<p>Another way to assign a value to a variable is to create a reference<br />
A reference is when one variable is an alias or pointer to another variable<br />
Changing one variable automatically changes the other<br />
To assign by reference, prepend & to the beginning of the old variable<br />
</p><br />";
$name = &$state;
echo "\$name is: $name. \$state is: ","$state<br />";



//Variable Variables (Dynamic Variables):
echo "A variable whose name is stored in another variable<br />
By using two dollar signs, the variable variable can access the value of the original variable<br />
For example: <br />";
$pet = "Bozo";
$clown = "pet";
echo $clown."<br />"; //prits pet
echo ${$clown}."<br />"; // prints Bozo

//isset() function
//Return true if a variable has been set and false otherwise
//If the variable has been set to NULL or has no value, it return false

$fname = NULL;
print "</br>isset" .isset($fname);


//is_null() function
//To see, if a variable has been set to NULL
print "</br>is_null".is_null($ fname);

//empty( ) function
//Return true if a variable does not exist, or exists and has been assigned one of the following: an empty string " ", 0 as a number, "0" as a string, NULL, or no value at all

 

//unset( ) function
//Unsets or destroys a given variable
//Can take varied number of arguments

?>

你可能感兴趣的:(PHP demo1)