LES FONCTIONS DE PHP


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;
}
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>
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>
Sortie:
My name is Kai Jim 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>
Sortie:
My name is Kai Jim 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>
Sortie:
1 + 16 = 17

17 commentaires:

  1. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog
    that 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

    ReplyDelete
  2. Nice blog! Is your theme custom made or did you download it from somewhere?
    A 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

    ReplyDelete
  3. 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
    assuming 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

    ReplyDelete
  4. Hello, i feel that i noticed you visited my web site so i got
    here 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

    ReplyDelete
  5. This paragraph is really a fastidious one it assists new web people, who are wishing for blogging.


    Also visit my website toe nail fungus treatments

    ReplyDelete
  6. I have been exploring for a little for any high quality articles or blog posts on this kind of area .
    Exploring 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

    ReplyDelete
  7. We stumbled over here by a different page and thought I may as well check things out.
    I 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

    ReplyDelete
  8. 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!
    Thanks!

    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/

    ReplyDelete
  9. Spot on with this write-up, I truly feel this amazing site needs
    much more attention. I'll probably be returning to see more, thanks for the info!

    Feel free to surf to my website :: house keeping agencies

    ReplyDelete
  10. of course like your web site however you need to check the spelling
    on 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

    ReplyDelete
  11. of course like your web site however you need to check the spelling on
    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.

    Take a look at my web-site housekeepingjobs
    Also see my website > office cleaning jobs

    ReplyDelete
  12. We stumbled over here by a different web page and thought
    I 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

    ReplyDelete
  13. Good article. I will be dealing with many of these issues as well.
    .

    My web site ... Slc-Wireless.Com

    ReplyDelete
  14. Pretty section of content. I just stumbled upon your website and
    in 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

    ReplyDelete
  15. Good post. I learn something new and challenging on websites I stumbleupon
    everyday. 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

    ReplyDelete
  16. Hmm it seems like your blog ate my first comment (it was
    super 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

    ReplyDelete
  17. Great post. I was checking constantly this blog and I am impressed!
    Very 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

    ReplyDelete

HELLO VISITORS THANKS FOR YOUR VISIT AND COMMENT