*.php
<?php
<?
<%
Running at https://www.csee.umbc.edu/~bwilk1/433/php_examples/simple.php
<!DOCTYPE html>
<?php $color = "#057f14" ?>
<html>
<head>
<style>
#example1{background-color:
<?php echo $color; ?>}
</style>
</head>
<body>
<p id="example1">My background is
<?php echo $color; ?></p>
</body>
</html>
Comments in PHP can either be
//
for single line/* */
for multiline#
for single line//This is a comment, nothing will happen
/*
This is also a comment
echo "HELLO"
won't print
*/
# This is a comment too
echo "HI"
$
(dollar sign)$a = "var";
$$a = "value";
$$$a = "whaaat";
echo $a;
echo $$a , $var;
echo $$$a, $$var, $value;
2001
-309
+33
0755
+010
0xFF
0xcc
3.14
0.016
0.314E1
16.0e-3
'This is a string'
"This is also a string"
false
true
0?'T':'F'
0.0?'T':'F'
516?'T':'F'
"0"?'T':'F'
"0.0"?'T':'F';
""?'T':'F'
(array())?'T':'F'
NULL?'T':'F'
(datatype) $var;
"5" * 2
'3.14' + 5
(int) "5"
(int) "4+16"
(int) "3.14"
(int) "19.99"
(float)"3.14"
(float) "3.14 is pi"
(string) 1
(string) true
(string) false
(bool) 0
(bool)3.14
(bool)1
(bool)""
(bool)"NO"
define
function, passing in a constant name and value$
define("NAME",value);
__LINE__
gives the current line of the file__FILE__
gives the file currently being executed__FUNCTION__
provides the function name__CLASS__
provides the class namedefine("PI",3.14);
define("TU", "TUESDAY");
echo "Sometimes " . PI . " day is on a " . TU;
echo "I am running from " . __LINE__;
$today = "Tuesday";
if($today == "Monday"){
echo "Happy Monday!";
}
else if($today == "Tuesday"){
echo "One Day Down!";
}
else{
echo "Start Counting Down Towards the Weekend!";
}
$today = "Tuesday";
if($today == "Monday"){
echo "Happy Monday!";
}
elseif($today == "Tuesday"){
echo "One Day Down!";
}
else{
echo "Start Counting Down Towards the Weekend!";
}
$today = "Tuesday";
$mon = "Monday";
switch($today){
case $mon:
echo "Happy Monday!";
break;
case "Tuesday":
echo "One Day Down!";
break;
default:
echo "Start Counting Down Towards the Weekend!";
}
Running at https://www.csee.umbc.edu/~bwilk1/433/php_examples/if.php
<?php
if(date('s') % 2 == 0){
?>
<p>You opened this page during an <strong>even</strong> second.
<?php
}
else{ ?>
<p>You opened this page during an <strong>odd</strong> second.
<?php } ?>
rand()
h1
tag with the words "It's Even!"h2
tag with the words "It's Odd!"${}
notation like in bash$lyric = "Teapot";
echo 'I\'m a little $lyric';
$lyric = "Teapot";
echo "I'm a little $lyric";
<<<
) followed by an identifier$var = "this";
$a_long_string = <<<HERE
This string is like a quoted
string, it will interpolate all
variables in it, like $var.
It only ends when HERE is on a
line of it's own
HERE;
echo $a_long_string;
.
).=
)<
,<=
,>
,>=
compare for lexicographic order, coerce if necessary$alphabet = "a" . 'b' . 'c' . 'd';
echo $alphabet;
$alphabet .= 'e';
echo $alphabet;
"3" == 3;
"3" === 3;
"3" <= 4;
$num1 = "15";
$num2 = 5;
$rest = "day of class";
$num = $num1 + $num2;
echo "Today is the ${num}th ". $rest;
explode
- Similar to string_split in other languageshtmlentities
- Encodes anything that needs to be encoded for HTML (e.g. <
, >
, etc.)html_entity_decode
- Converts HTML encoded characters back into their regular string valuesjoin
- Joins an array together (can also use implode
)parse_str
- Turns a query string into an arraystrip_tags
- Removes all HTML and PHP tags from a stringtrim
- Removes whitespace from both ends of the stringecho print_r(explode(',', "John,Paul,George,Ringo"),true);
$html = "This is an example of an HTML tag 3 < 4 <html>. It must be closed like this: </html>";
echo htmlentities($html);
$html = "Some characters, like the amperstand, need to be escaped in HTML, like &";
echo html_entity_decode($html);
parse_str("key1=value1&key2=value2",$result);
echo print_r($result,true);
$wiki = <<<WIKI
<div class="thumbinner" style="width:302px;"><a href="/wiki/File:Manchester_Mark2.jpg" class="image"><img alt="" src="//upload.wikimedia.org/wikipedia/en/thumb/d/d8/Manchester_Mark2.jpg/300px-Manchester_Mark2.jpg" width="300" height="238" class="thumbimage" srcset="//upload.wikimedia.org/wikipedia/en/d/d8/Manchester_Mark2.jpg 1.5x" data-file-width="355" data-file-height="282" /></a>
<div class="thumbcaption">
<div class="magnify"><a href="/wiki/File:Manchester_Mark2.jpg" class="internal" title="Enlarge"></a></div>
The Manchester Mark 1 was one of the world's first stored-program computers.</div>
</div>
</div>
<p>The <b>Manchester Mark 1</b> was one of the earliest <a href="/wiki/Stored-program_computer" title="Stored-program computer">stored-program computers</a>, developed at the <a href="/wiki/Victoria_University_of_Manchester" title="Victoria University of Manchester">Victoria University of Manchester</a> from the <a href="/wiki/Manchester_Small-Scale_Experimental_Machine" title="Manchester Small-Scale Experimental Machine">Small-Scale Experimental Machine</a> (SSEM) or "Baby" (operational in June 1948). It was also called the <b>Manchester Automatic Digital Machine</b>, or <b>MADM</b>.<sup id="cite_ref-FOOTNOTELavington199820_1-0" class="reference"><a href="#cite_note-FOOTNOTELavington199820-1">[1]</a></sup> Work began in August 1948, and the first version was operational by April 1949; a program written to search for <a href="/wiki/Mersenne_primes" class="mw-redirect" title="Mersenne primes">Mersenne primes</a> ran error-free for nine hours on the night of 16/17 June 1949.</p>
<p>The machine's successful operation was widely reported in the British press, which used the phrase "electronic brain" in describing it to their readers. That description provoked a reaction from the head of the University of Manchester's Department of Neurosurgery, the start of a long-running debate as to whether an electronic computer could ever be truly creative.</p>
WIKI;
echo strip_tags($wiki);
$evil_user = "I am going to try an run some code <?php echo 'Hi There';?>";
echo strip_tags($evil_user);
$str_with_spaces = " There are so many spaces on either side of me ";
echo trim($str_with_spaces);
chmod a+r
<?php
version of the tag