Skip to Content
PHP and MySQL® 24-Hour Trainer
book

PHP and MySQL® 24-Hour Trainer

by Andrea Tarr
November 2011
Beginner
504 pages
11h 30m
English
Wrox
Content preview from PHP and MySQL® 24-Hour Trainer

Lesson 9

Learning about Scope

In this lesson you learn about the concept of scope. As you learn about user-defined functions in Lesson 10 and objects in Lesson 12, you learn the details of how scope works when you use functions and objects.

Scope refers to where a specific variable, function, or object can be seen. Scope can be local or global.

Learning about Local Variables

Think of a program as a village where the houses have blinds on all their windows. Imagine that a variable is a person taking a walk down the street. He can talk and interact with all the other people outside, but he cannot be seen by anyone inside the houses nor can he see them. If he needs to talk with someone inside a house, he has to knock on the door and be let in first.

This is the way that local scope works. Variables can be seen only where they are created. The houses are the user-defined functions you learn about in Lesson 10. If you create a variable outside a function, it cannot be seen inside the function. Conversely, if you create a variable inside a function, it can be seen only within that function.

Global scope, on the other hand, is as if that village was on a South Seas island and the houses were open-air tents. Everyone can see everyone else at all times.

Scope is local by default in PHP. Having local scope allows for encapsulation. Encapsulation means that you can create a function and know that nothing that goes on outside of that function will change anything inside the function unexpectedly. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Expert PHP and MySQL®

Expert PHP and MySQL®

Andrew Curioso, Ronald Bradford, Patrick Galbraith

Publisher Resources

ISBN: 9781118066881Purchase book