Jump to content

functions


jwk811

Recommended Posts

are functions basically variables? what makes a function different than a variable? if its because they carry on variables then what about the ones that dont carry variables?

you know how functions return either a true or a false? how do i find out what it returned? say i have a script full of functions and one is checkLogin(); which will check to see if the user is logged in or not and perform different actions depending on if that is "true" or "false".. would i go if(checkLogin() == false) { redirect } ? something like that?

thanks for any help at all! ill take whatever i can get!  ;D
Link to comment
https://forums.phpfreaks.com/topic/27155-functions/
Share on other sites

There are also predefined functions in PHP such as the date(), time(), and mail() functions. But as it has already been stated, if you create your own functions, it is basically just a set of intructions/code/conditions, that you can call from diferent areas of your code without having to write it again. If you're going to use functions, I suggest you look into using classes.  ;)
Link to comment
https://forums.phpfreaks.com/topic/27155-functions/#findComment-124146
Share on other sites

[code]<?
function name($lName){
    if($lName == "Bill"){
          $firstName = "Red";
    }else{
          $firstName = "Ted";
    }
    return $firstName;
}
$lastName = "Joe";
echo name($lastName);
?>[/code]
If you were to run that code, This line: [b]echo name($lastName);[/b] will output "Ted" if you change the value of lastName, your output will be "Red".
Link to comment
https://forums.phpfreaks.com/topic/27155-functions/#findComment-124160
Share on other sites

Functions are the mechanism by which you combine programming statements into conceptual wholes.

Let's say that every page on your website has to start with the same 10 lines of code.  Do you really want to retype that same cover over and over on every page?  If your site is only 3 pages, it's not a huge deal.  But if your site is 10 pages, thats 100 lines of code.  Now what happens if you find an error in it?  You have to change it on every single page.  Sucks for you!

Here comes functions to the rescue!  You write that code [b]once[/b] as a function in a single file.  Then you require_once that file in every page and at the very top of every page, call the function you wrote.

Think of a function as a call to directory service.  You dial the phone, give the operator your zip code or a company name, and they give you a phone number.  You don't care [b]how[/b] they found that information, all you care about is that they gave it back to you.

That's what a function is.  Every page doesn't care [b]how[/b] the header is created or necessarily what's in it.  All they care about is if they can call the function that creates it and all [b]you[/b] as the programmer care about is that you don't have to retype it a dozen or more times and that when you do find an error it's localized to one piece of your code.
Link to comment
https://forums.phpfreaks.com/topic/27155-functions/#findComment-124162
Share on other sites

thanks i see.. but what about the return true or false thing? heres a function i have that returns either true or false.. what happens after that when i can the function?
[code]
function checkLogin(){
  /* Check if user has been remembered */
  if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
      $_SESSION['username'] = $_COOKIE['cookname'];
      $_SESSION['password'] = $_COOKIE['cookpass'];
  }

  /* Username and password have been set */
  if(isset($_SESSION['username']) && isset($_SESSION['password'])){
      /* Confirm that username and password are valid */
      if(confirmUser($_SESSION['username'], $_SESSION['password']) != 0){
        /* Variables are incorrect, user not logged in */
        unset($_SESSION['username']);
        unset($_SESSION['password']);
        return false;
      }
      return true;
  }
  /* User not logged in */
  else{
      return false;
  }
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/27155-functions/#findComment-124164
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.