PSA4 Technical Predefined Functions PDF
PSA4 Technical Predefined Functions PDF
PRE-SUMMATIVE ASSESSMENT
4
PHP PRE DEFINED FUNCTIONS
Section: TR31
PHP Constants
Constants are like variables except that once they are defined they cannot
be changed or undefined.
A constant is an identifier (name) for a simple value. The value cannot be
Applications Development and Emerging Page 2 of
Technologies 11
changed during the script.
Syntax
define(name, value, case-insensitive)
Parameters:
• name: Specifies the name of the constant
• value: Specifies the value of the constant
• case-insensitive: Specifies whether the constant name should be case-
insensitive. Default is false
Example
Create a constant with a case-sensitive name:
<?php
define("GREETING", "Welcome to PHP Programming!");
echo GREETING;
?>
Including files is very useful when you want to include the same PHP, HTML,
or text on multiple pages of a website.
The include and require statements are identical, except upon failure:
• require will produce a fatal error (E_COMPILE_ERROR) and stop the script
• include will only produce a warning (E_WARNING) and the script will
continue
So, if you want the execution to go on and show users the output, even if the
include file is missing, use the include statement. Otherwise, in case of
FrameWork, CMS, or a complex PHP application coding, always use the require
statement to include a key file to the flow of execution. This will help avoid
compromising your application's security and integrity, just in-case one key file
is accidentally missing.
Including files saves a lot of work. This means that you can create a standard
header, footer, or menu file for all your web pages. Then, when the header
needs to be updated, you can only update the header include file.
Syntax
include 'filename';
or
require 'filename';
<?php
echo "<p>Copyright © 1999-" . date("Y") . " W3Schools.com</p>";
?>
</body>
</html>
Example 2
Assume we have a standard menu file called "menu.php":
<?php
echo '<a href="/default.asp">Home</a> -
<a href="/html/default.asp">HTML Tutorial</a> -
<a href="/css/default.asp">CSS Tutorial</a> -
<a href="/js/default.asp">JavaScript Tutorial</a> -
<a href="default.asp">PHP Tutorial</a>';
?>
All pages in the Web site should use this menu file. Here is how it can be done
(we are using a <div> element so that the menu easily can be styled with CSS
later):
Example
<html>
<body>
<div class="menu">
<?php include 'menu.php';?>
</div>
Example 3
Assume we have a file called "vars.php", with some variables defined:
<?php
$color='red';
$car='BMW';
?>
Then, if we include the "vars.php" file, the variables can be used in the calling
file:
Example
<html>
<body>
</body>
</html>
However, there is one big difference between include and require; when a file is
included with the include statement and PHP cannot find it, the script will
continue to execute:
Example
<html>
<body>
</body>
</html>
If we do the same example using the require statement, the echo statement will
not be executed because the script execution dies after the require statement
returned a fatal error:
Example
<html>
<body>
</body>
</html>
Example
Return the length of the string "Hello world!":
<?php
echo strlen("Hello world!"); // outputs 12
?>
Example
Count the number of word in the string "Hello world!":
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
Example
Reverse the string "Hello world!":
<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
Example
Search for the text "world" in the string "Hello world!":
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
Example
Replace the text "world" with "Dolly":
<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello
Dolly!
?>
1. Create 5 different short story webpages and Convert your stories into a web
based form, use include() and require() functions to connect all pages
integrate with HTML and CSS (Put images on the stories). See the format
below:
Snip and paste your source codes here. Snip it directly from the IDE so that colors
of the codes are preserved for readability. Include additional pages if necessary.
1. https://www.w3schools.com/css/
2. https://www.w3schools.com/html/
3. https://www.w3schools.com/php/php_variables.asp
4. https://www.w3schools.com/php/php_constants.asp
5. https://www.w3schools.com/php/php_includes.asp
6. https://www.w3schools.com/php/php_string.asp