0% found this document useful (0 votes)
14 views18 pages

ADI302 - Fundamentos - Programación - WEB - Sesión 13 - 20240731

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

ADI302 - Fundamentos - Programación - WEB - Sesión 13 - 20240731

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Sesión 13 –

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();

• Three string functions:


<?php
echo strrev(" .dlrow olleH"); // Reverse string
echo str_repeat("Hip ", 2); // Repeat string
echo strtoupper("hooray!"); // String to uppercase
?>
Defining a Function
• The general syntax for a function is as follows:
function function_name([parameter [, ...]])
{
// Statements
}
• The first line of the syntax indicates the following:
• A definition starts with the word function.
• A name follows, which must start with a letter or underscore, followed by any number of
letters, numbers, or underscores.
• The parentheses are required.
• One or more parameters, separated by commas, are optional (as indicated by the square
brackets).
Returning a Value
• Cleaning up a full name:
<?php
echo fix_names("WILLIAM", "henry", "gatES");

function fix_names($n1, $n2, $n3)


{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));

return $n1 . " " . $n2 . " " . $n3;


}
?>
Returning an Array
• Returning multiple values in an array:
<?php
$names = fix_names("WILLIAM", "henry", "gatES");

echo $names[0] . " " . $names[1] . " " . $names[2];

function fix_names($n1, $n2, $n3)


{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));

return array($n1, $n2, $n3);


}
?>
Passing Arguments by Reference
• Passing values to a function by reference:
<?php
$a1 = "WILLIAM";
$a2 = "henry";
$a3 = "gatES";

echo $a1 . " " . $a2 . " " . $a3 . "<br>";


fix_names($a1, $a2, $a3);
echo $a1 . " " . $a2 . " " . $a3;

function fix_names(&$n1, &$n2, &$n3)


{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));
}
?>
Returning Global Variables
• Returning values in global variables:
<?php
$a1 = "WILLIAM";
$a2 = "henry";
$a3 = "gatES";

echo $a1 . " " . $a2 . " " . $a3 . "<br>";


fix_names();
echo $a1 . " " . $a2 . " " . $a3;

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";
}
?>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy