ADI302 - Fundamentos - Programación - WEB - Sesión 13 - 20240731
ADI302 - Fundamentos - Programación - WEB - Sesión 13 - 20240731
Advanced PHP
Clase: FUNDAMENTOS DE PROGRAMACIÓN WEB
(ADI302)
Objetivos
• Continuar con el aprendizaje del lenguaje PHP.
• Incluir PHP en un archivo HTML.
PHP Functions and Objects
• The basic requirements of any programming language include somewhere to
store data, a means of directing program flow, and a few bits and pieces such as
expresión evaluation, file management, and text output.
• PHP has all these, plus tools like else and elseif to make life easier.
• But even with all these in your toolkit, programming can be clumsy and tedious,
especially if you have to rewrite portions of very similar code each time you need
them.
• That’s where functions and objects come in. As you might guess, a function is a
set of statements that performs a particular function and—optionally—returns a
value.
PHP Functions and Objects
• Functions have many advantages over contiguous, inline code.
• For example, they:
• Involve less typing
• Reduce syntax and other programming errors
• Decrease the loading time of program files
• Decrease execution time, because each function is compiled only once, no
matter how often you call it
• Accept arguments and can therefore be used for general as well as specific
cases
PHP Functions and Objects
• Objects take this concept a step further. An object incorporates one or
more functions, and the data they use, into a single structure called a
class.
PHP Functions
• PHP comes with hundreds of ready-made, built-in functions, making it a very rich
language.
• To use a function, call it by name. For example, you can see the date function in
action here:
echo date("l"); // Displays the day of the week
• The parentheses tell PHP that you’re referring to a function. Otherwise, it thinks
you’re referring to a constant.
PHP Functions
• Functions can take any number of arguments, including zero. For example,
phpinfo, as shown next, displays lots of information about the current installation
of PHP and requires no argument.
phpinfo();
function fix_names()
{
global $a1; $a1 = ucfirst(strtolower($a1));
global $a2; $a2 = ucfirst(strtolower($a2));
global $a3; $a3 = ucfirst(strtolower($a3));
}
?>
Recap of Variable Scope
• Local variables are accessible just from the part of your code where you define
them. If they’re outside of a function, they can be accessed by all code outside of
functions, classes, and so on. If a variable is inside a function, only that function
can access the variable, and its value is lost when the function returns.
• Global variables are accessible from all parts of your code.
• Static variables are accessible only within the function that declared them but
retain their value over multiple calls.
Including and Requiring Files
• As you progress in your use of PHP programming, you are likely to start building a
library of functions that you think you will need again.
• You’ll also probably start using libraries created by other programmers.
• There’s no need to copy and paste these functions into your code.
• You can save them in separate files and use commands to pull them in.
• There are two commands to perform this action: include and require.
The include Statement
• Using include, you can tell PHP to fetch a particular file and load all its contents.
• It’s as if you pasted the included file into the current file at the insertion point.
• Including a PHP file:
<?php
include "library.php";
// Your code goes here
?>
Using include_once
• Including a PHP file only once:
<?php
include_once "library.php";
// Your code goes here
?>
• Then, any further attempts to include the same file (with include or
include_once) will be ignored.
Using require and require_once
• A potential problem with include and include_once is that PHP will only attempt
to include the requested file.
• Program execution continues even if the file is not found.
• When it is absolutely essential to include a file, require it.
• Requiring a PHP file only once:
<?php
require_once "library.php";
// Your code goes here
?>
PHP Version Compatibility
• If you need to check whether a particular function is available to your code, you
can use the function_exists function, which checks all predefined and user-
created functions.
• Checking for a function’s existence:
<?php
if (function_exists("array_combine"))
{
echo "Function exists";
}
else
{
echo "Function does not exist - better write our own";
}
?>