Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

PHP Tutorials – Functions (Part I)

By Exforsys | on September 29, 2006 |
PHP Tutorial

PHP Tutorials – Functions (Part I)

In this PHP Tutorial you will learn about PHP Functions – Part 1, Function syntax, example, parameters and variable scope.

Sometimes you need to perform an operation more than once in the code, to do so, you can use functions,

A function is declared using the following syntax:

function function_name([parameters])

The name of the function must follow the rules of the identifier, because it’s an identifier that identifies this function.

Function can have zero to many parameters, parameters are used as variables thought the function code block.

The function code block starts with a curly brace, and ends with a curly brace.

Example:

    <?php
        function get_days($week) {
            return $week * 7;
        }
        $weak = 15;
        echo get_days($week);
    ?>

The code is to get the number of days of the year until the end of this week, to calculate the number of days, you have to know the week number and then multiply it by 7, so multiplying the week number by 7 won’t be changed, and it can be used many times to get the number of days for any week, and so a separate function is made to calculate the number of days.

In order for the function to be able to calculate the number of days, it needs to know the week number, so it’s a parameter for the function, no one can call this function without passing the week number.

A new feature of functions is the ability to return values, as we can see here the get_days function made the calculation and returned the calculation result, so it can be used by the script.

The script defined a value for the week number and called the function using the week number, the function returned the number of days, and the script printed it.

The variable name can be different than the function parameter name, the function parameter name is just to tell that to use this function, you have to pass parameters to it.

In the previous example, the $weak value is passed to the function, only the value, so if we printed the variable $weak after its value was passed to the function, it would print 15.

Functions can assign default values for any of its parameters, so if user didn’t pass a value for that parameter, it will use the default value.

Example:

    <?php
        function get_days($week = 1) {
        return $week * 7;
        }
        echo get_days();
    ?>

No parameters were passed to the function, so it will use the default value, which is 1, and will return 7.

Variables scope:

Variables defined out of any functions are called global variables, for most programming languages, global variables are available for functions, so functions are able to changes their values freely.

This is not the case for PHP, functions can’t access global variables directly.

Example:

    <?php
        $week = 12;
        echo get_days();
        function get_days() {
        $weeks = $week + 3;
        return $week * 7;
        }
    ?>   

$week inside the function is different than the global $week, it’s not initialized at the function, so it will be substituted by NULL, and will return zero when multiplied by 7.

If you want to use the global variables inside functions, you have to use the global statement.

Example:

    <?php
        $week = 12;
        echo get_days();
        function get_days() {
        global $week;
        $weeks = $week + 3;
        return $week * 7;
        }
    ?>

This way the function will use the global $week, and will return 105.

Any operation on the global variable will affect it’s value, so after this function the global variable value will be 15.

« « Sample Resume – Principal Resume
PHP Tutorials – Loops » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 401 Followers
  • Popular
  • Recent
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • PHP Strings

    March 18, 2009 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP Tutorial : PHP & MySQL

    January 16, 2007 - 0 Comment
  • PHP Tutorial – Introduction

    April 29, 2006 - 0 Comment
  • PHP Tutorials – Variables

    September 22, 2006 - 0 Comment
  • PHP Tutorial – Installation

    May 17, 2006 - 0 Comment
  • PHP Tutorials – Operators (Part I)

    September 18, 2006 - 0 Comment
  • PHP Tutorial – Syntax

    May 17, 2006 - 0 Comment
  • PHP Strings

    March 18, 2009 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP and MySQL User Registration

    October 31, 2008 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorial : PHP & MySQL

    January 16, 2007 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Arrays (Part 2)

    November 15, 2006 - 0 Comment
  • PHP Tutorials : Arrays

    November 5, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • PHP Strings
  • How to Use Cookies in PHP
  • PHP and MySQL User Registration
  • WAMP Server
  • PHP Tutorial : PHP & MySQL

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2022. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy
Necessary Always Enabled