PHP Fonctions
La vraie puissance de PHP vient de ses fonctions.
En PHP, il ya plus de 700 fonctions intégrées.
PHP intégré dans les fonctions
Pour une référence complète et des exemples de fonctions intégrées, s'il vous plaît visitez notreréférence PHP.
Fonctions PHP
Dans ce chapitre, nous allons vous montrer comment créer vos propres fonctions.
Pour conserver le script d'être exécuté lorsque la page se charge, vous pouvez le mettre dans une fonction.
Une fonction est exécutée par un appel à la fonction.
Vous pouvez appeler une fonction à partir de n'importe où dans une page.
Créer une fonction PHP
Une fonction est exécutée par un appel à la fonction.
Syntaxe
function functionName()
{
code to be executed;
}
{
code to be executed;
}
Les lignes directrices de la fonction PHP:
- Donner la fonction d'un nom qui reflète ce que la fonction ne
- Le nom de la fonction peut commencer par une lettre ou un underscore (pas un nombre)
Exemple
Une fonction simple qui écrit mon nom quand il est appelé:
<html>
<body>
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
</body>
</html>
<body>
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
</body>
</html>
Sortie:
My name is Kai Jim Refsnes
Fonctions PHP - Ajout de paramètres
Pour ajouter plus de fonctionnalités à une fonction, on peut ajouter des paramètres. Un paramètre est tout comme une variable.
Les paramètres sont spécifiés après le nom de la fonction, à l'intérieur des parenthèses.
Par exemple une
L'exemple suivant va écrire différents prénoms, mais égale le nom de famille:
<html>
<body>
<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>
</body>
</html>
<body>
<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>
</body>
</html>
Sortie:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
Exemple 2
La fonction suivante a deux paramètres:
<html>
<body>
<?php
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeName("Kai Jim",".");
echo "My sister's name is ";
writeName("Hege","!");
echo "My brother's name is ";
writeName("Ståle","?");
?>
</body>
</html>
<body>
<?php
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeName("Kai Jim",".");
echo "My sister's name is ";
writeName("Hege","!");
echo "My brother's name is ";
writeName("Ståle","?");
?>
</body>
</html>
Sortie:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?
Fonctions PHP - les valeurs de retour
Pour laisser une fonction retourne une valeur, utilisez l'instruction de retour.
Exemple
<html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>
Sortie:
1 + 16 = 17
Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog
ReplyDeletethat automatically tweet my newest twitter updates.
I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.
Feel free to visit my blog post zetaclear side effects
Nice blog! Is your theme custom made or did you download it from somewhere?
ReplyDeleteA design like yours with a few simple adjustements would really make my blog jump out.
Please let me know where you got your theme. Appreciate it
Stop by my page: hardwood floors
I know this if off topic but I'm looking into starting my own blog and was curious what all is required to get set up? I'm
ReplyDeleteassuming having a blog like yours would cost a pretty penny?
I'm not very web savvy so I'm not 100% sure. Any recommendations or advice would be greatly appreciated. Kudos
hardwood floors installation
Here is my web page - engineered hardwood flooring
Hello, i feel that i noticed you visited my web site so i got
ReplyDeletehere to return the favor?.I'm trying to find issues to enhance my site!I suppose its adequate to make use of a few of your ideas!!
Here is my blog post :: discount hardwood floors
This paragraph is really a fastidious one it assists new web people, who are wishing for blogging.
ReplyDeleteAlso visit my website toe nail fungus treatments
I have been exploring for a little for any high quality articles or blog posts on this kind of area .
ReplyDeleteExploring in Yahoo I at last stumbled upon this website.
Studying this information So i am glad to express
that I have an incredibly excellent uncanny feeling I found
out just what I needed. I most no doubt will make certain to
do not forget this web site and provides it
a look on a relentless basis.
my blog ... hardwood floors
My website :: hard wood floor
We stumbled over here by a different page and thought I may as well check things out.
ReplyDeleteI like what I see so now i'm following you. Look forward to looking over your web page again.
Here is my web-site ... treatment for toenail fungus
Also see my web site > toe nail fungus treatments
I was suggested this web site by my cousin. I'm not sure whether this post is written by him as nobody else know such detailed about my difficulty. You're incredible!
ReplyDeleteThanks!
Also visit my web page; house keeping services
my page - teikii.info/groups/important-factors-explaining-exactly-why-a-maid-service-is-something-you-should-go-for/
Spot on with this write-up, I truly feel this amazing site needs
ReplyDeletemuch more attention. I'll probably be returning to see more, thanks for the info!
Feel free to surf to my website :: house keeping agencies
of course like your web site however you need to check the spelling
ReplyDeleteon several of your posts. A number of them are rife with spelling issues and I to find it very troublesome to inform the truth however I'll definitely come again again.
Feel free to surf to my page housekeepingjobs
of course like your web site however you need to check the spelling on
ReplyDeleteseveral of your posts. A number of them are rife with spelling issues and I to find it very
troublesome to inform the truth however I'll definitely come again again.
Take a look at my web-site housekeepingjobs
Also see my website > office cleaning jobs
We stumbled over here by a different web page and thought
ReplyDeleteI should check things out. I like what I see so now i
am following you. Look forward to finding out about your web
page again.
Here is my weblog :: zetaclear scam
Good article. I will be dealing with many of these issues as well.
ReplyDelete.
My web site ... Slc-Wireless.Com
Pretty section of content. I just stumbled upon your website and
ReplyDeletein accession capital to assert that I get actually enjoyed
account your blog posts. Any way I'll be subscribing to your augment and even I achievement you access consistently quickly.
my web site; Sac Louis Vuitton
Good post. I learn something new and challenging on websites I stumbleupon
ReplyDeleteeveryday. It's always helpful to read articles from other writers and use a little something from their websites.
Feel free to surf to my blog :: Louis Vuitton Purses Outlet
Hmm it seems like your blog ate my first comment (it was
ReplyDeletesuper long) so I guess I'll just sum it up what I submitted and say, I'm thoroughly enjoying your blog.
I as well am an aspiring blog blogger but I'm still new to the whole thing. Do you have any points for novice blog writers? I'd certainly appreciate it.
Also visit my blog post :: Cheap NFL Jerseys
Great post. I was checking constantly this blog and I am impressed!
ReplyDeleteVery helpful info particularly the last part :) I care for such info a lot.
I was seeking this particular information for a very long time.
Thank you and good luck.
my page ... Sac Louis Vuitton