Web Design/Dynamic Websites/Simple Functions in PHP
< Web Design < Dynamic Websites
Web Design → Simple Functions in PHP
|
Note: Work in progress PHP.net has a reference on PHP functions, but it's a little complicated for your average beginner.
Example PHP file with function
Try testing the following PHP file:
<?php // // A simple function that prints a welcome message // function welcome() { print("<h2>Hi there. Welcome to PHP functions</h2>"); print("<p>Here we'll learn a little about functions</p>"); } welcome(); welcome(); welcome(); ?>
Can you explain why it does what it does?
Example 2: a function with an argument
A small modification:
<?php // // A simple function that prints a welcome message // function welcome($username) { print("<h2>Hi there $username. Welcome to PHP functions</h2>"); print("<p>Here we'll learn a little about functions</p>"); } welcome("Michael"); welcome("Izaak"); welcome("Brigitte"); ?>
Can you explain what's happening this time?
Example 3: Something more useful
So far we've done simple PHP templating using include files. But we can replace this with functions to, and give ourselves some extra power while we're at it! Try the following:
<?php // // A simple function that prints the code that's at the // beginning of every page on my site. // function printHeader() { ?> <html> <head> <title>Home page of my site</title> </head> <body> <h1> My website </h1> <div id="main"> <?php } printHeader(); print("<p> The content of my page would go here</p>"); // printFooter(); // woops, we don't have this function yet! ?>
Again, test out this page and then see if you can explain what it's doing!
Here's the challenge:
- See if you can modify your printHeader() function so that you can say: printHeader("Page 2"); - and the text "Page 2" will be displayed in the title tags (look back to a previous example!)
- Try creating and using your own printFooter() function.
Of course, we'd remove these functions into an include-file so that we can reuse them on other pages!
HTML: What is HTML, Advanced HTML Tutorial, HTML Challenges, Lists and Tables, The Structure of HTML, Pen and Paper HTML activities
CSS: Learning Basic HTML and CSS, CSS challenges, Embedded CSS, Basic HTML and CSS Tutorial, Inline CSS, Develop cascading style sheets, CSS3 Animations
PHP: Simple functions in PHP, Dynamic websites, PHP challenges, Dynamic websites with PHP, Generating a receipt with PHP, Responding to HTML forms with PHP,
JavaScript: An Introduction to Programming with JavaScript, JavaScript Challenges, Getting to know JavaScript events, Getting to know the Document Object Model with JavaScript, JavaScript pen-n-paper activities, Web Programming Intro, Produce basic client side script for dynamic web pages
XML: XML challenges
Databases: Creating Database Tables
Projects: A small website project, A step-by-step web design project, Build a basic web page, Build a small website
Challenges: Web design, HTML, AJAX, CSS, JavaScript, PHP, XML, Information Architecture, Algorithms
Activities: Web design group learning activities, Pen and Paper HTML activities, A Web Design Quiz show, Ordering Tasks for a Web Project, Paired Bio Pages, Scoping a Small Project, Emerging Technologies Research Activities
Project management: An introduction to Project Management, Developing a Client Project
Design: Accessibility, Design Principles for Web Design, Design Suggestions
Testing: Testing Website performance, Website Performance Test Plan
Publishing: Getting Your Site On the Web
Certification and standards: Cert IV Website Qualifications in Australia, Australian Qualifications, Web design qualifications
Professional development: Contribute to personal skill development and learning, Useful Applications, Useful Books, Emerging Technologies, Using the Internet as a Learning Tool
Course meta-information: Web Design, About the web design learning project, New Structure, Sample Timetable