PHP: Examples and Variables

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Examples and Variables

Aims:

PHP's built-in functions

news.php: illustrates date function

<body>

<div id="banner">
<?php 
  echo date('D F j Y, h:i:s a');
?>
</div>

...

</body>
</html>

Footers

Footers

Notes on include

Variables in PHP

answer.php: a script that uses a variable

<body>
<?php $answer = 42; ?>

<p>
 What is the answer to Life, the Universe, and Everything?
 <?php echo $answer; ?>
</p>

</body>
</html>

Assignment

Numeric types

Non-numeric types

Static and strong versus dynamic and weak

Compare sections.html...

<body>
<h1>A History of Lobotomies</h1>

<h2>Section 1: Introduction</h2>

<p>Blah, blah, blah.</p>

<h2>Section 2: 19th Century Lobotomies</h2>

<p>Blah, blah, blah.</p>

...
</body>
</html>

...with sections.php

<body>
<h1>A History of Lobotomies</h1>

<?php $sectionNum = 0; ?>

<?php $sectionNum = $sectionNum + 1; ?>
<h2>Section <?php echo $sectionNum; ?>: Introduction</h2>

<p>Blah, blah, blah.</p>

<?php $sectionNum = $sectionNum + 1; ?>
<h2>Section <?php echo $sectionNum; ?>: 19th Century Lobotomies</h2>

<p>Blah, blah, blah.</p>

...
</body>
</html>