PHP Crash Course: Learn Modern PHP & Master the Fundamentals (Step-By-Step) — Transcript
Full transcript
- 0:04They say PHP is dead, but here's the
- 0:06thing. It still powers over 70% of the
- 0:08web. From WordPress to modern web apps,
- 0:11PHP is still very much alive, and
- 0:14learning it is a great way to break into
- 0:15back-end development. And that's what
- 0:17this PHP crash course is all about,
- 0:19giving you a practical, modern
- 0:21introduction to PHP. By the end, you'll
- 0:24have the skills to build your own PHP
- 0:26applications. Your instructor is Luis
- 0:28Ramirez, a software engineer with over a
- 0:31decade of experience building secure,
- 0:33scalable, productionready applications.
- 0:35Luis has taught over thousands of
- 0:37students how to not just write PHP,
- 0:39build their own apps, and help them get
- 0:40hired, but his students truly understand
- 0:42the why behind the code, setting them
- 0:45apart from many PHP developers. I almost
- 0:47forgot, we're giving away a huge bonus
- 0:50for anyone taking this crash course.
- 0:51free access to Zero to Mastery's Discord
- 0:54community where you can hang out with
- 0:55Luis mentors and fellow students.
- 0:58Normally, this is reserved for ZTM
- 1:00Academy members, but for a limited time,
- 1:02we're giving you access so that you
- 1:03don't have to learn alone and that you
- 1:05can get direct help from ZTM mentors and
- 1:08instructors. The invite link is in the
- 1:10description down below. So, hit pause,
- 1:12go find that link, join the server, say
- 1:15hi, and then come on back. All done. All
- 1:18right, let me hand it over to Luis so
- 1:19that you can start learning PHP. Enjoy.
- 1:22[Music]
- 1:31[Music]
- 1:33PHP is one of the best languages you can
- 1:35learn. Now, hold on a moment. That's a
- 1:37controversial statement. A quick online
- 1:40search yields hundreds of results
- 1:42declaring PHP a dead language. Over the
- 1:45years, many programmers have developed a
- 1:47misconception about PHP. Let's examine
- 1:50the facts to decide whether PHP is a
- 1:53perfect fit for you. First and foremost,
- 1:5580% of sites are powered by PHP. Can you
- 1:58believe it? That pretty much means that
- 2:01eight out of the 10 websites you visit
- 2:03are using PHP under the hood, thus
- 2:05solidifying itself as a reliable
- 2:07language for the web. On top of being a
- 2:10popular language, performance is another
- 2:12strong point. Over the years,
- 2:14maintainers have taken every step to
- 2:16improve the speed of PHP. Without a
- 2:19doubt, those efforts have paid off. Apps
- 2:21have seen significant improvement in
- 2:23performance with each release. Lastly,
- 2:26new features are constantly introduced
- 2:29in the language. Since PHP has wide
- 2:31adoption, the PHP team can tailor and
- 2:34fine-tune the language for the
- 2:36community. They've introduced features
- 2:37that can be found in most modern
- 2:39languages. These features can help us
- 2:41write better, more efficient code. There
- 2:44has to be a catch, right? After all,
- 2:47developers are avoiding PHP for a
- 2:49reason. PHP is one of the most
- 2:51beginnerfriendly languages available.
- 2:53Within a weekend, deploying a site from
- 2:55development to production is possible.
- 2:58Unfortunately, this aspect of the
- 3:00language is a double-edged sword.
- 3:02Security vulnerabilities can easily be
- 3:04introduced into a site. As a beginner,
- 3:07you should always take the proper
- 3:08precautions for writing secure code.
- 3:11More often than not, beginners tend to
- 3:13overlook basic security features. Worst
- 3:16of all, terrible programming practices
- 3:18can easily plague an application.
- 3:20Stealing an application should always be
- 3:22one of your top priorities. That's
- 3:24difficult to do when the code is poorly
- 3:26written. Luckily, you're taking this
- 3:29course. My goal for this course is to
- 3:31help you become a proficient PHP
- 3:33developer. Unlike other courses that
- 3:35teach you the basics and send you on
- 3:37your way, this course covers a wide
- 3:39variety of topics starting from the
- 3:41basics of the language to covering
- 3:43advanced features such as security and
- 3:46MVC architecture. If you're interested
- 3:49in a complete overview, I highly
- 3:51recommend checking out the course
- 3:52description. As we progress through the
- 3:55course, I'll be sure to point out best
- 3:57practices. This way, you can avoid
- 3:59common pitfalls most beginners fall into
- 4:02during their journey. I know you're
- 4:03antsy to get started, but there's one
- 4:05thing you should know before digging
- 4:07into the course. This course assumes you
- 4:09have knowledge of HTML and CSS. Our
- 4:13focus is primarily going to be on PHP.
- 4:15To save time, I'll provide most of the
- 4:18HTML and CSS without reviewing every
- 4:21single line of code. You're expected to
- 4:23understand basic HTML syntax. In
- 4:26addition, JavaScript would be nice to
- 4:28know, but not required. JavaScript is
- 4:31not a language we'll be using. heavily.
- 4:33If you know JavaScript, that'll come in
- 4:35handy. Otherwise, don't worry about it.
- 4:38All right, it's time to start learning.
- 4:40I hope you're excited. It's going to be
- 4:42a long but worthwhile journey. When
- 4:44you're ready, I'll see you in the next
- 4:46lecture.
- 4:47[Music]
- 4:58In this lecture, we're going to start
- 5:00preparing an environment for PHP. It's
- 5:02going to be easier than you think. But
- 5:04what is an environment? Why is it
- 5:06important to have one? Let's demystify
- 5:09this concept before taking a step
- 5:11further. Suppose we were gardening. What
- 5:13does a garden need? There needs to be
- 5:15soil, sunlight, and water. Without the
- 5:18proper environment, our plants would not
- 5:20be able to flourish. It's absolutely
- 5:23critical to cultivate land for growing
- 5:25plants and food. The concept of
- 5:28preparing an environment for gardening
- 5:30can be applied to the programming world.
- 5:32Applications need the proper environment
- 5:34for running code. In the programming
- 5:36world, an environment is the machine
- 5:38where the application runs. In most
- 5:41cases, applications rely on additional
- 5:43software. Without the proper tools or
- 5:46programs, our applications won't work.
- 5:49Generally speaking, there are two types
- 5:51of environments which are local and
- 5:53production environments. A local
- 5:56environment refers to the setup you have
- 5:58on your personal computer or local
- 6:00machine where you can write, test, and
- 6:02run your code. Local environments are
- 6:05primarily used for development and
- 6:07testing purposes. This allows you, the
- 6:10developer, to break things without
- 6:12disrupting your users. You can freely
- 6:14modify and make changes before shipping
- 6:17a program to the world. On the other
- 6:19hand, a production environment is a live
- 6:22environment where your code is actually
- 6:24deployed and running for end users. A
- 6:26production environment is typically
- 6:28hosted on a remote server. It is
- 6:31designed to handle the demands of
- 6:33realworld usage including security,
- 6:36scalability, and reliability. The
- 6:38production environment is also
- 6:40monitored, managed, and maintained by a
- 6:43team of professionals to ensure optimal
- 6:45performance. It's not the place to test
- 6:47our code. If we accidentally upload
- 6:50errorprone code, our users will be
- 6:52affected by our actions.
- 6:56As you can imagine, we will be working
- 6:58in a local environment. Setting up an
- 7:00environment can take a bit of time.
- 7:02Luckily, the PHP community has taken the
- 7:05time to provide solutions for quickly
- 7:07stirring up PHP environments. In the
- 7:10resource section of this lecture, I
- 7:12provide links to the most popular
- 7:14solutions. Developers have a plethora of
- 7:17options for installing PHP. There are
- 7:20dozens of programs online to easily
- 7:22prepare an environment for running PHP
- 7:25on a local machine. You may have
- 7:27encountered them such as XamP,
- 7:30M or Laragon. While these programs are
- 7:34helpful, we're going to take a different
- 7:36approach. One of the links in the
- 7:38resource section takes you to a site
- 7:40called Replet. Replet is a service for
- 7:43creating environments for various
- 7:45programming languages. Best of all, our
- 7:48environments are completely accessible
- 7:49from the browser. We do not need to
- 7:52install programs on our machine to run
- 7:54PHP. I find that Replet is the easiest
- 7:57solution for beginners to get started
- 7:59with PHP. Here's what I want you to do.
- 8:02Create an account on Replet and log in
- 8:04into the dashboard. I'm going to quickly
- 8:06sign into my account. I'll see you in a
- 8:09moment.
- 8:12All right. After signing up for an
- 8:14account, let's create an environment.
- 8:17Keep in mind environments created on
- 8:19Replet are meant to be for development
- 8:21purposes. The applications built on this
- 8:24site are not meant to be publicly hosted
- 8:26for the world. In the future, we'll
- 8:28discuss some options for publicly
- 8:30hosting your app. For now, let's shift
- 8:33our focus to creating a new environment.
- 8:36At the top left corner, there's a button
- 8:38called create. Click on it. A pop-up is
- 8:41going to appear to ask us a few
- 8:43questions. Firstly, we must select a
- 8:46template. As I mentioned earlier, Replet
- 8:49supports multiple programming languages.
- 8:51From the template option, we can search
- 8:54for PHP. A few options may appear. The
- 8:57option we're interested in is called PHP
- 9:00web server. Make sure to select this
- 9:02option. Otherwise, you may encounter
- 9:05errors when going through the course.
- 9:07Next, we must provide a name for our new
- 9:10project. Let's call it PHP fundamentals.
- 9:15Lastly, let's create the project. Replet
- 9:18starts preparing our environment on our
- 9:21behalf. This process may take a few
- 9:23moments. Just be patient.
- 9:27After all has been said and done, our
- 9:29environment is ready. Let's make a few
- 9:31tweaks before writing code. At the
- 9:34bottom left corner, there's a list of
- 9:35tools available. From this section,
- 9:38there's an option to modify the settings
- 9:40of the project's code editor. In my
- 9:43opinion, the font size is too small. You
- 9:45may feel the same. On the settings tab,
- 9:48there's an option for enlarging the
- 9:49font. Select a font size that's suitable
- 9:52for your needs. Hopefully, you can read
- 9:55the code on my screen. Great. We're
- 9:57ready to run the project. At the top of
- 9:59the page, click the run button.
- 10:04Behind the scenes, Replet takes the time
- 10:06to launch our PHP application. After a
- 10:09few moments, a new tab appears for
- 10:12previewing our application. The message
- 10:14hello world renders on our screens.
- 10:17Isn't that awesome? With a few clicks,
- 10:19we've got a working environment. Below
- 10:21the preview, there's a section called
- 10:23console. The console is a feature we
- 10:26don't need to worry about. It's taking
- 10:28up unnecessary room on our screens.
- 10:31Fortunately, we can click on the web
- 10:33view portion to drag it around. If we
- 10:36hover this section of the editor with
- 10:38the console, it'll get merged with the
- 10:40console as a tab. I recommend moving
- 10:43things around to reduce clutter. All we
- 10:45need is the file tree, code editor, and
- 10:48preview. Everything else will be
- 10:50explored as we learn more about PHP.
- 10:54If you have experience with PHP, you may
- 10:57be curious as to why I haven't mentioned
- 11:00Docker. Docker is a tool for managing
- 11:02environments on your machine.
- 11:04Professional PHP developers use Docker
- 11:07for their projects. So, why aren't we
- 11:09using it? The reason is simple. Docker
- 11:12is very complicated for beginners. I
- 11:15want us to focus on the basics of PHP
- 11:18before diving into larger, more complex
- 11:20tools. I consider Docker to be a great
- 11:23option. For now, Replet will suffice.
- 11:28It's time to finally start coding. For
- 11:30the rest of this course, I'm going to
- 11:32assume you have the PHP server running
- 11:34unless I state otherwise. When you're
- 11:37ready, I'll see you in the next section.
- 11:40[Music]
- 11:50In this lecture, we're going to dive
- 11:52into programming theory. More precisely,
- 11:55we're going to talk about what PHP is
- 11:58doing when we write code. To kick this
- 12:00discussion off, I think we should take
- 12:02the time to define PHP. That's going to
- 12:05make sure we're all on the same page.
- 12:07Here's the best definition of PHP
- 12:10available. PHP is an open-source
- 12:13generalpurpose scripting language for
- 12:15developing web applications. Wo, what
- 12:18does all that mean? Let's slowly unpack
- 12:20that definition to fully grasp PHP.
- 12:24First and foremost, open-source
- 12:26software, OSS for short, refers to code
- 12:29that has been freely released to the
- 12:31public. Anyone can study, modify, and
- 12:34redistribute the software. Collaborators
- 12:37from all over the world have voluntarily
- 12:39contributed their time to improving PHP.
- 12:42It's one of the largest open-source
- 12:44software projects in the world.
- 12:47You may be curious, does this mean that
- 12:50you can download the original code for
- 12:52PHP and modify it? The answer is yes,
- 12:54you can. In the resource section of this
- 12:57lecture, I provide a link to the
- 12:59official PHP GitHub account. On this
- 13:02page, you're going to find various links
- 13:04related to the PHP project. Everything
- 13:07is available to the public free of
- 13:09charge. If you don't like where PHP is
- 13:12heading, you're more than welcome to
- 13:13modify the language to your heart's
- 13:15content. However, most people are happy
- 13:18with PHP, so that probably won't be
- 13:20necessary. Regardless, it's still good
- 13:23to know where PHP is discussed and
- 13:26maintained.
- 13:28Moving forward, let's keep dissecting
- 13:30the definition of PHP. I mentioned that
- 13:33PHP is a generalpurpose language. A
- 13:36generalpurpose language is a programming
- 13:38language that can be utilized to develop
- 13:40different kinds of programs. Originally,
- 13:43PHP was introduced as a language for
- 13:45developing web applications. As time
- 13:48passed, developers discovered it could
- 13:50be used for other things. It's possible
- 13:53to develop command line scripts, desktop
- 13:55applications, image processing
- 13:57libraries, and so much more. Programming
- 14:00languages are considered to be general
- 14:02purpose when they can be used to solve a
- 14:04wide array of problems. For all intents
- 14:07and purposes, this course primarily
- 14:09focuses on web development with PHP.
- 14:12Other areas of software development
- 14:14aren't explored. you're more likely to
- 14:17be hired as a PHP web developer than a
- 14:19PHP desktop application developer.
- 14:22Lastly, PHP is considered to be a
- 14:25scripting language because it's
- 14:26interpreted. Technically, it's not wrong
- 14:29to call it a programming language. After
- 14:31all, all scripting languages are
- 14:33programming languages, but the reverse
- 14:36isn't true. Not all programming
- 14:38languages are scripting languages. That
- 14:40leads us to the question, what does it
- 14:42mean when a programming language is
- 14:44interpreted? If you've ever watched a
- 14:46movie or TV show about computers, you
- 14:49may have seen something like this. What
- 14:51you're being shown is called binary code
- 14:53or machine code. Binary code is the only
- 14:56language that computers can understand.
- 14:59As you can imagine, reading binary code
- 15:01is not the most ideal situation to find
- 15:04yourself in. For this reason,
- 15:06programming languages were introduced to
- 15:08improve readability. On the other hand,
- 15:11a new problem arises from using
- 15:13programming languages. Since computers
- 15:16can't understand programming languages,
- 15:18code must be transformed into machine
- 15:20code. This is where a program called a
- 15:23compiler comes into play. The job of a
- 15:25compiler is to transform our code into
- 15:28machine code. After our code has been
- 15:31transformed, the instructions written
- 15:33from our program can be executed by a
- 15:35machine. This concept is not exclusive
- 15:38to PHP. Other programming languages
- 15:40share this feature, whether it's Python,
- 15:43C++, or Rust. So, what does this have to
- 15:46do with an interpreter? There are two
- 15:49main approaches to generating machine
- 15:51code. You can use either a compiler or
- 15:54an interpreter. A compiler and an
- 15:56interpreter are tools used to translate
- 15:59computer code into a form that a
- 16:01computer can execute. The main
- 16:03difference between the two is how they
- 16:05do it. A compiler translates the entire
- 16:08code of a program into machine code in
- 16:11one go and saves it as an executable
- 16:13file. This file can then be run as many
- 16:16times as needed without recompiling it.
- 16:19An interpreter, on the other hand,
- 16:21executes the code line by line,
- 16:23translating each line of code into
- 16:25machine code and then executing it
- 16:27immediately. It does not generate an
- 16:30executable file. Here's another way of
- 16:32thinking about it. A compiler is like a
- 16:34translator who translates a book into
- 16:37another language and then gives you the
- 16:38translated book to read. An interpreter
- 16:41is like a translator who translates a
- 16:43sentence and then reads it to you
- 16:45immediately. So you may be wondering, is
- 16:48PHP compiled or interpreted? PHP is
- 16:52interpreted. Programming languages that
- 16:54are interpreted are considered to be
- 16:56scripting languages. Therefore, PHP is a
- 16:59scripting language. With that being
- 17:02said, there are debates about which
- 17:04approach is better. Should programmers
- 17:06use compiled languages or interpreted
- 17:09languages? Those discussions are beyond
- 17:11the scope of this course. All you need
- 17:13to know is that PHP is interpreted. By
- 17:16installing PHP, we have access to the
- 17:19PHP interpreter for transforming our
- 17:22code into machine code. The next step in
- 17:24our journey is to start using the PHP
- 17:26interpreter. In the next lecture, let's
- 17:29start writing our first PHP script to
- 17:31run it through the PHP interpreter.
- 17:35[Music]
- 17:45In this lecture, we're going to execute
- 17:47our first PHP script. This means we'll
- 17:51create a PHP file, run it through the
- 17:53PHP interpreter, and view the output.
- 17:56The question is, how can we use the PHP
- 17:59interpreter? We're going to use a server
- 18:01for performing this job. Often times, I
- 18:04find that beginners confuse web servers
- 18:06and the PHP interpreter. The job of the
- 18:09PHP interpreter is to transform our code
- 18:12into machine code. On the other hand,
- 18:15the job of a web server is to deliver
- 18:17files. To better understand, let's look
- 18:19at an example. In a real environment, we
- 18:22may have dozens of files. Servers may
- 18:25contain a mixture of HTML, CSS, and
- 18:28image files. In our browsers, we have
- 18:31the power to request specific files from
- 18:33a server. For example, let's say we
- 18:36wanted the about.html
- 18:38file. A request from the browser is sent
- 18:41to the web server. The job of the web
- 18:43server is to find the file specified in
- 18:46the URL. If a file is found, the file
- 18:49gets sent back to the browser. Lastly,
- 18:52the browser renders the file on the
- 18:54screen. Overall, it's a straightforward
- 18:56process. However, there's one problem
- 18:58with this process. Websites that deliver
- 19:01the same files to the browser are known
- 19:04as static websites. It doesn't matter
- 19:06how many times visitors refresh the
- 19:08page, they're delivered the same files.
- 19:11Nowadays, most websites are dynamic.
- 19:14Rather than delivering the same page,
- 19:16pages can be tailored to our users to
- 19:18provide a unique experience. Websites
- 19:21that generate unique content are known
- 19:23as dynamic websites. Web servers can be
- 19:26configured to process PHP files. For
- 19:29example, let's say our server has PHP
- 19:32files. In the browser, we can request a
- 19:35PHP file from the server. Our server
- 19:38searches for the file to deliver to the
- 19:40browser. So far, the process is very
- 19:43similar to static websites. Afterward,
- 19:46things begin to take an alternative
- 19:48route. Web servers don't immediately
- 19:50send the PHP file back to the browser.
- 19:53Instead, servers are capable of running
- 19:56the file through the PHP interpreter.
- 19:58We'll wait for the interpreter to
- 20:00generate HTML. Once an HTML page has
- 20:04been created, the file gets sent to the
- 20:06browser to be rendered on the visitor's
- 20:08machine. Understanding this process is
- 20:11absolutely crucial. PHP does not run in
- 20:14the browser. Servers are meant to
- 20:16execute and handle the output of a PHP
- 20:18file.
- 20:21Fortunately for us, we're using Replet.
- 20:23Behind the scenes, a server has been
- 20:26configured to deliver files. At the same
- 20:28time, if we request a PHP file, it'll be
- 20:32processed through the PHP interpreter.
- 20:34Let's try demoing this process. In the
- 20:37file tree, open the index.php file. PHP
- 20:41files must have the PHP extension.
- 20:44Otherwise, web servers will not run the
- 20:47file through the PHP interpreter. If the
- 20:50server encounters a nonPHP file, it'll
- 20:53automatically get sent to the browser
- 20:55without further processing. Inside this
- 20:58file, there may be initial code. It's
- 21:00completely fine to leave the file empty.
- 21:03We're not going to need the starter code
- 21:05provided by Replet. Now, the question
- 21:07is, what kind of code can be written
- 21:10from within a PHP file? Believe it or
- 21:13not, we can write valid HTML code. All
- 21:16HTML features are completely supported
- 21:18in PHP files. After all, PHP files get
- 21:22transformed into HTML files. It only
- 21:25makes sense that HTML syntax is
- 21:28supported. For example, let's try adding
- 21:31a pair of H1 tags with the message hello
- 21:34world.
- 21:36Original, I know, but just keep
- 21:39following along. After adding the H1
- 21:41tag, we must refresh the preview by
- 21:44pressing the refresh icon. Make sure
- 21:46you're refreshing the preview, not the
- 21:48browser. If you were to refresh the
- 21:50browser, the entire editor gets
- 21:52reloaded. I know it can be annoying to
- 21:54have to constantly refresh the preview.
- 21:57There's a shortcut for forcing a refresh
- 21:59on your keyboard. You can press controls
- 22:02shortcut to save the file. For Mac
- 22:04users, this command would be commands.
- 22:07By default, Replet autos saves your
- 22:10files. If you were to trigger the save
- 22:12action, the preview automatically
- 22:14refreshes. It's a simple trick that you
- 22:17should remember going forward. As you
- 22:19can see, our message appears in the
- 22:20preview. Cool, right? As a simple
- 22:23exercise, let's try creating another PHP
- 22:26file. In the file tree, create a new
- 22:28file called about.php.
- 22:34Once again, make sure the file has the
- 22:36PHP extension. Otherwise, our code won't
- 22:39be processed through the PHP
- 22:41interpreter. Next, inside the about.php
- 22:45file, let's write a simple message to
- 22:47help us identify the file in the
- 22:49browser.
- 22:52So far so good. Now, the question is,
- 22:55how can we preview the contents of the
- 22:57about.php file? Our preview continues to
- 23:00display the message from the index.php.
- 23:03PHP file. To view the contents of the
- 23:05about file, we must update the URL.
- 23:08Unfortunately, this isn't possible
- 23:11directly from the preview. We must open
- 23:13a new tab to view this information. On
- 23:16the right side of the web view tab,
- 23:18there's a button for opening the site in
- 23:20a new tab. Click on it.
- 23:24As you can see, Replet provides a free
- 23:27URL for previewing our site. At the end
- 23:30of the URL, add the following about.php.
- 23:36After inputting this path, the contents
- 23:38of the about file are rendered. Voila,
- 23:41we've successfully created another page.
- 23:44You may be wondering, how does the
- 23:46server know to deliver the index.php
- 23:48file when a file is not specified? If I
- 23:51were to remove the file name from the
- 23:53path, the contents of the index.php
- 23:56appear. Strange, right? Well, not
- 23:59really. By default, most web servers are
- 24:02configured to deliver an index.php
- 24:05or index.html
- 24:07file to the browser if a file is not
- 24:09specified. This is why we're able to
- 24:11view the contents of the index.php file.
- 24:15That's about it. So far, PHP doesn't
- 24:18seem all that different from HTML. Don't
- 24:21worry, in the next set of lectures,
- 24:23we're going to get an opportunity to
- 24:25generate dynamic content. When you're
- 24:27ready, I'll see you in the next one.
- 24:29[Music]
- 24:40In this lecture, we're going to start
- 24:42entering PHP mode from our files.
- 24:45Starting from where we left off in the
- 24:47previous lecture, we're going to
- 24:48continue modifying the index.php file.
- 24:52Actually, we're not going to need to
- 24:54view other files for a while. To reduce
- 24:57distraction, I recommend closing the
- 24:59sidebar by pressing the button in the
- 25:01top left corner.
- 25:05Closing the sidebar leaves us with the
- 25:07code editor and preview. Awesome. Now,
- 25:10what am I talking about when I say PHP
- 25:12mode? Well, if we were to write HTML
- 25:15inside a PHP file, the PHP interpreter
- 25:18does not process the HTML portion of our
- 25:21file. That's not exactly what we want.
- 25:24We want the PHP interpreter to execute
- 25:27instructions written from our file. In
- 25:29order to do so, a pair of PHP tags must
- 25:32be added to the file. Follow along with
- 25:35me. At the top of the file, add the
- 25:37following characters. less than question
- 25:40mark PHP space question mark greater
- 25:44than.
- 25:46Congrats, you've created your first pair
- 25:49of PHP tags. By adding these tags, we're
- 25:52entering PHP mode. Entering PHP mode
- 25:55allows our machines to begin executing
- 25:58instructions. These instructions can
- 26:00vary from sending an email, processing a
- 26:03transaction, or registering a new user
- 26:05into our system. PHP tags are always
- 26:09necessary. We must always tell the PHP
- 26:12interpreter which portion of the PHP
- 26:15file contains instructions and which
- 26:17portion contains regular HTML. As you
- 26:20might have already guessed, multiple PHP
- 26:23tags can be inserted into the file. For
- 26:26example, let's add another pair at the
- 26:29end of the file.
- 26:32Refreshing the page results in the same
- 26:34output as before. That's to be expected
- 26:37since we haven't written instructions
- 26:39from within the PHP tags. Before we do,
- 26:42there are a few more things worth
- 26:44mentioning. Firstly, PHP tags can be
- 26:48written from within HTML tags. As an
- 26:50example, let's replace the text inside
- 26:53the H1 tags with a pair of PHP tags.
- 26:58The text disappears from the preview.
- 27:01This is considered completely valid PHP
- 27:04code. Being able to write PHP inside
- 27:07HTML gives us the power to generate
- 27:10dynamic content in various locations of
- 27:13our HTML page. The second thing I wanted
- 27:16to mention was being unable to do the
- 27:18reverse. This time let's move the H1
- 27:21tags inside the PHP tags.
- 27:26A different result gets produced in the
- 27:28preview. An error gets rendered by the
- 27:31PHP interpreter. But why? This is
- 27:33because the rules of PHP mode are
- 27:36different from HTML mode.
- 27:40Inside PHP tags we must write valid
- 27:43code. In the English language, there are
- 27:45rules for how English is read and
- 27:47written. There's a specific structure we
- 27:50must follow for writing English, such as
- 27:52ending a sentence with a period or
- 27:54capitalizing the first word of a
- 27:56sentence. The rules for human languages
- 27:59are referred to as grammar. In a similar
- 28:02sense, programming languages have rules
- 28:04for how they're read and written. These
- 28:07rules are referred to as a programming
- 28:09languages syntax. This is why we're
- 28:11getting an error. We're not following
- 28:13PHP's syntax rules. As a result, the PHP
- 28:17interpreter is unhappy. In the next
- 28:19lecture, let's start learning PHP's
- 28:22syntax rules.
- 28:24[Music]
- 28:35In this lecture, we're going to start
- 28:37writing PHP code by learning about the
- 28:39echo keyword. First off, what is a
- 28:42keyword? Keywords are a feature in PHP
- 28:44for performing specific actions. If we
- 28:47type a keyword, the PHP interpreter
- 28:50understands that we're trying to perform
- 28:52a specific action based on that keyword.
- 28:55PHP offers various keywords. In the
- 28:58resource section of this lecture, I
- 29:00provide a link to an official list of
- 29:02keywords from the PHP documentation.
- 29:05Throughout this course, we're going to
- 29:07explore most of these keywords. Starting
- 29:09with the echo keyword. Let's head over
- 29:12to our editors. Previously, we added a
- 29:15few PHP tags. I think this is a bit
- 29:17excessive. Let's remove all of the tags
- 29:20except the opening tag.
- 29:24But why are we leaving just the opening
- 29:26tag? Don't we need a closing tag, too?
- 29:29Here's the cool thing about PHP. If we
- 29:31don't plan on exiting PHP mode, we don't
- 29:34need to add a closing PHP tag. A closing
- 29:37PHP tag becomes optional when we only
- 29:40intend to write PHP code. In most cases,
- 29:44developers often omit the closing PHP
- 29:46tags for files that only contain PHP
- 29:49code for readability. After removing the
- 29:52tags, let's write the echo keyword.
- 29:57So, what does the echo keyword do? The
- 30:00echo keyword instructs the interpreter
- 30:02to output content. Developers like using
- 30:04this keyword to output text. For
- 30:07example, let's add the following. Space
- 30:09quote hello world quote semicolon.
- 30:15Pay close attention to the types of
- 30:17quotes I'm using. I writing the message
- 30:19with double quotes, not single quotes.
- 30:22After adding this text, we can refresh
- 30:24the page to view the output. The message
- 30:26written after the echo keyword appears
- 30:29on the page. Pretty cool. But what's up
- 30:31with the strange syntax after the echo
- 30:34keyword? Let's break it down. Firstly,
- 30:36our message must be wrapped with quotes.
- 30:39This is how PHP is able to understand
- 30:42that we're trying to write raw text. Do
- 30:44you remember the last time we wrote HTML
- 30:47inside our PHP tags? PHP didn't like
- 30:50that. Previously, we had to adhere to
- 30:52the syntax rules of PHP. By using
- 30:55quotes, we're allowed to write whatever
- 30:57we want without PHP complaining. In this
- 31:01case, we're writing hello world. Lastly,
- 31:03we're adding a semicolon character. This
- 31:06character denotes the end of a
- 31:08statement.
- 31:11In my opinion, one of the hardest
- 31:13aspects of programming is learning the
- 31:15terminology. It's half the battle. A
- 31:18statement is another way of describing a
- 31:20single instruction given to a machine.
- 31:22In most cases, statements can be a
- 31:24single line of code. Programming
- 31:26languages give us the ability to
- 31:28communicate with a machine. Instructions
- 31:31can be given to a machine to perform
- 31:33various tasks from sending an email to
- 31:36processing a transaction. You can think
- 31:38of a statement as a single sentence in
- 31:41the English language. Multiple sentences
- 31:43can be combined to create a book. In a
- 31:46similar sense, multiple statements can
- 31:48be combined to create an application.
- 31:53So what's the difference between a
- 31:55keyword and a statement? A keyword
- 31:58refers to a single word in a line of
- 32:00code, whereas a statement describes the
- 32:03entire line of code. Back to the task at
- 32:06hand. We added a semicolon character to
- 32:09tell the PHP interpreter that we're
- 32:11finished with this line of code. PHP
- 32:13doesn't know when you're finished with a
- 32:15specific instruction. You must
- 32:17explicitly inform PHP when you're
- 32:19finished with an instruction. Once
- 32:21you've added the semicolon character,
- 32:23PHP moves on to the next instruction.
- 32:26So, for example, let's write another
- 32:28echo statement. This time, I'm going to
- 32:31write the following message. My name is
- 32:33John.
- 32:35In the preview, our new message gets
- 32:38rendered. You may have noticed, but PHP
- 32:40executes our code in the order the
- 32:42instructions are written. Statements are
- 32:45executed from top to bottom, similar to
- 32:47how books are read from top to bottom.
- 32:49The order of your code does matter.
- 32:54All right, I think it's time for an
- 32:55exercise. This exercise is fairly
- 32:58straightforward. I want you to wrap the
- 33:00echo statements we've written with a
- 33:02pair of H1 tags. That's it. Pause the
- 33:05video and give the exercise a try. Good
- 33:08luck.
- 33:11Welcome back. Hopefully, you were able
- 33:13to tackle the exercise. If not, that's
- 33:16fine. Let's walk through the solution
- 33:18together. First, let's close the PHP tag
- 33:21since we're going to write HTML.
- 33:26Next, wrap the PHP tags with a pair of
- 33:28H1 tags.
- 33:34That's it. The preview shows us that the
- 33:37text has been wrapped with H1 tags
- 33:39because the text is enlarged. Now, you
- 33:42may have come up with an alternative
- 33:44solution which is just as valid. An
- 33:46alternative solution would have been to
- 33:48write the H1 tags from within the
- 33:50quotes. I'll quickly undo the changes I
- 33:53made to my file.
- 33:56Next, I'll wrap the hello world message
- 33:59with H1 tags. These tags should be
- 34:02inserted from within the quotes, not
- 34:03outside.
- 34:06This time only the first message gets
- 34:09enlarged. So which solution is better?
- 34:12In our case, it doesn't matter. You're
- 34:14going to see both solutions in the real
- 34:16world. It's not uncommon to write PHP
- 34:19inside HTML tags or to write HTML tags
- 34:23from within an echo statement. Either
- 34:25solution is valid. Let's continue our
- 34:28journey in the next lecture. I'll see
- 34:30you there.
- 34:31[Music]
- 34:41In this lecture, we're going to learn
- 34:43another feature of PHP, which are
- 34:45comments. As you already know, comments
- 34:48in HTML were designed for adding notes
- 34:50in our code. They serve the same purpose
- 34:53in PHP. The main difference is how
- 34:55they're written. PHP supports three
- 34:58variations for comments. The first
- 35:01syntax for comments is two forward slash
- 35:03characters. After these characters, we
- 35:06can write whatever we want. We can even
- 35:08type gibberish. Comments are not
- 35:10processed by the PHP interpreter.
- 35:12Therefore, anything can be written
- 35:14inside a comment. Let's add a comment
- 35:17above the first echo statement.
- 35:21The comment we've written is considered
- 35:23a single line comment. As the name
- 35:25implies, only the code on this line is
- 35:27affected by the comment. If I were to
- 35:29attempt to write more text on a new
- 35:32line, errors would get thrown. In some
- 35:34cases, you may want to write a long
- 35:36comment. Technically, we could write a
- 35:38comment on a single line, but that would
- 35:40be harder to read. This dilemma leads us
- 35:42to the following type of comment called
- 35:44a multi-line comment. They're written
- 35:47with the following characters. slash
- 35:49asterisk asterisk slash.
- 35:54In between these characters, we're
- 35:56allowed to break our comments into
- 35:57multiple lines. As long as our text
- 36:00falls between these characters, they're
- 36:02considered to be within the bounds of
- 36:03the comment. This type of comment is
- 36:06called a multi-line comment. They were
- 36:08designed for longer comments. There's
- 36:10one more syntax I want to show you.
- 36:12There's an alternative syntax for
- 36:14writing a singleline comment. Rather
- 36:17than using forward slash characters, you
- 36:19can use a hash character.
- 36:23This character also denotes a single
- 36:25line comment. It's not as popular as
- 36:27other solutions, but you may come across
- 36:29it in a preview. Nothing appears aside
- 36:31from the messages in the echo
- 36:33statements. Comments are completely
- 36:35ignored by the PHP interpreter. Overall,
- 36:38comments are a feature for documenting
- 36:40our application. Throughout the course,
- 36:42I'll be using comments to add additional
- 36:44notes to our code. In the next lecture,
- 36:46let's start talking about variables.
- 36:49[Music]
- 36:59In this lecture, we're going to start
- 37:01using variables for storing data. So
- 37:04far, we've written comments. They're
- 37:06nice and all, but they're not all PHP
- 37:08has to offer. Programmers are often
- 37:10dealing with data. Data can range from
- 37:13addresses to the price of a product.
- 37:15Often, most pages on the web rely on
- 37:17data. Otherwise, we'd be staring at the
- 37:20same page. PHP has a feature called
- 37:22variables for storing data. A common
- 37:25analogy for variables is boxes. We can
- 37:28use boxes for storing physical items. If
- 37:31you're moving, it's common to label
- 37:32boxes to describe the contents of the
- 37:34box. For instance, if a box contains
- 37:37dishes and utensils, you may label the
- 37:40box as kitchen. The inner contents of
- 37:42the box can be considered the value.
- 37:44Variables are similar to boxes from the
- 37:47real world. Unlike boxes, variables
- 37:49don't store physical items. Instead,
- 37:52they can store file data, form data, or
- 37:55data from a database. They're extremely
- 37:57flexible for storing any kind of data.
- 38:01Let's try creating a variable in the
- 38:04index file. I'm going to clear the
- 38:06contents of the file except for the
- 38:08opening PHP tag. Everything else isn't
- 38:11necessary. After doing so, type the
- 38:13following dollar sign age semicolon.
- 38:18There are a few rules for writing
- 38:20variables in PHP. Variables must begin
- 38:23with a dollar sign character. By typing
- 38:26this character, PHP allocates memory on
- 38:28a user's machine to store data. This
- 38:31symbol is followed by the name of the
- 38:33variable.
- 38:35Variable names follow strict rules.
- 38:37Firstly, names are allowed to contain
- 38:40alphabetic characters, numbers, and
- 38:42underscores. Secondly, special
- 38:44characters are not allowed. For example,
- 38:47the at symbol can't be used as a
- 38:49variable name. Lastly, the first
- 38:51character of a variable name may be a
- 38:54letter or underscore. While numbers are
- 38:56allowed in variable names, they cannot
- 38:58be the first character. Besides these
- 39:01rules, PHP gives us a lot of freedom in
- 39:04naming our variables. There are a few
- 39:06good practices worth mentioning.
- 39:08Firstly, variable names should be
- 39:10descriptive. For example, let's say we
- 39:13had a variable for storing the total
- 39:15price of a user's purchase. The name of
- 39:17the variable could be called total
- 39:20price. That name is short, clear, and
- 39:22concise. A bad example would be an
- 39:24abbreviation called TP. We might
- 39:27understand what that means, but what
- 39:29would our co-workers think of the name?
- 39:31If you're working on a team, your team
- 39:33members may not be able to understand
- 39:35the purpose of the variable. Descriptive
- 39:37names provide clarity. In some cases,
- 39:41you may need to use abbreviations for
- 39:43variables with long names. If that's the
- 39:45case, your abbreviations should be
- 39:48easily recognizable. For example, we may
- 39:51need to store the name of a customer. A
- 39:53good abbreviation would be cust name
- 39:56where cust is short for customer.
- 40:00However, using the name cust n is harder
- 40:03to recognize. Another useful tip is to
- 40:06avoid using single letters as variable
- 40:09names. This circles back to the first
- 40:11tip. Using a single letter doesn't
- 40:13describe the type of data stored in a
- 40:15variable. With that being said, some
- 40:17developers will use a single letter when
- 40:20dealing with loops. That's the only
- 40:22scenario where using single letters is
- 40:24fine. In most cases, you should stick to
- 40:27using longer descriptive names. Being
- 40:30consistent is another good habit to pick
- 40:32up. Your variable names should have
- 40:34consistent spelling. Mixing the casing
- 40:37of your variables can result in
- 40:39confusing and unreadable code. Last but
- 40:42not least, you should avoid generic
- 40:44names. For example, you may need to
- 40:46store user data in a variable. It may be
- 40:49tempting to call the variable data, but
- 40:51that doesn't exactly describe what type
- 40:53of data you're storing. Are you storing
- 40:56file data, database data, or maybe
- 40:58you're storing product data? The more
- 41:00precise you are, the better. Overall,
- 41:03these tips should help you with naming
- 41:05your variables. What if you need to
- 41:07write multiple words from within a
- 41:09variable name? As mentioned before, we
- 41:11can't use special characters such as
- 41:13spaces. Developers have come up with
- 41:16three naming conventions for these
- 41:18scenarios. The first naming convention
- 41:20is called camelcasing. In this format,
- 41:23every word is capitalized except for the
- 41:25first word. By doing so, it's easier to
- 41:28read the words in a variable name. Up
- 41:30next, we have a format called Pascal
- 41:33Casing, which is similar to camel
- 41:34casing. Every word is capitalized,
- 41:37including the first word. Lastly, snake
- 41:40casing is another format that uses
- 41:42lowercase letters. Instead of
- 41:44capitalizing each word, words are
- 41:46separated with underscores. You're
- 41:48allowed to use whatever naming
- 41:50convention suits your application.
- 41:52Generally speaking, most PHP developers
- 41:54prefer camel casing for variables.
- 41:57However, if you decide to use a
- 41:59different naming convention, that's
- 42:01perfectly fine. The most important thing
- 42:03is to be consistent. For this course,
- 42:06I'll be sticking with camel casing for
- 42:08multi-worded variable names. We've
- 42:10successfully created our first variable.
- 42:13Currently, it doesn't store anything. In
- 42:15the next set of lectures, let's start
- 42:17storing data.
- 42:19[Music]
- 42:30In this lecture, we're going to store a
- 42:32value inside the variable we created
- 42:34from the previous lecture. We can
- 42:36accomplish this task by using an
- 42:38operator. To be more specific, the
- 42:40assignment operator. But what is an
- 42:42operator? An operator is a symbol or
- 42:45character that accepts a value. This
- 42:48value then gets produced into an
- 42:50entirely new value. This is important to
- 42:53understand. Operators always create a
- 42:56brand new value from an existing value.
- 43:00In the resource section of this lecture,
- 43:02I provide a link to PHP's documentation
- 43:05page for its operators. Out of the box,
- 43:08dozens of operators are available for
- 43:10various actions. We're going to be
- 43:12exploring most of them throughout the
- 43:14course. To get started, let's take a
- 43:16look at the assignment operator. The job
- 43:19of the assignment operator is to store a
- 43:22value in a variable. Back in our
- 43:24project, we have a variable called age,
- 43:26which doesn't store anything. Let's
- 43:28update the variable by typing the
- 43:30following after the name of the
- 43:32variable. space equals space 29.
- 43:37In this example, we're storing a number.
- 43:39Unlike raw text, we don't have to wrap
- 43:42numbers with quotes. They're optional
- 43:44whenever we're dealing with numbers.
- 43:46Next, let's try echoing the age.
- 43:50In the preview, the age gets displayed.
- 43:53Perfect. We've got a variable for
- 43:55storing the age. But how is it working?
- 43:57The equal sign character is known as the
- 44:00assignment operator. This operator can
- 44:02be used to store a value inside a
- 44:05variable. The variable name must always
- 44:07appear to the left of the operator. The
- 44:10value must appear to the right. Creating
- 44:13variables allows us to reference a value
- 44:15anywhere in our script. In our case,
- 44:18we're referencing the value from the
- 44:19echo statement. Previously, we would
- 44:22type the value immediately after the
- 44:24echo keyword. This time, we're
- 44:27referencing the variable by typing its
- 44:29name. Whenever variables are created,
- 44:32PHP stores the value in its memory.
- 44:34Afterward, if PHP encounters a reference
- 44:38to a variable, it'll replace the
- 44:40reference with the value stored within
- 44:42the variable. In a nutshell, typing echo
- 44:45age is the same as typing echo 29.
- 44:50Hopefully, you can see the benefits of
- 44:52variables. Storing values in variables
- 44:55is more efficient as we can reference
- 44:57the value multiple times anywhere in our
- 45:00script. Things get even more interesting
- 45:02when trying to update a variable. For
- 45:05example, let's say my birthday pops up.
- 45:07This means my age should change.
- 45:10Variables can be updated the same way
- 45:12they're created. On a new line, type the
- 45:14following dollar sign age equals 30
- 45:18semicolon.
- 45:21PHP is smart enough to update the
- 45:23existing variable to the new value. We
- 45:25don't have to worry about our programs
- 45:27hogging resources whenever a variable
- 45:29needs to be updated. Isn't that awesome?
- 45:32PHP is capable of being efficient with
- 45:34our programs variables. The number 30
- 45:37appears in the preview, thus proving
- 45:39that our variable was updated. Before I
- 45:42end this lecture, there's one warning I
- 45:44want to put out there. As we know,
- 45:46variables are case-sensitive. It can be
- 45:49easy to create a variable when you
- 45:51originally intended to update a
- 45:53variable. For example, let's say I typed
- 45:56the word age with a capital A.
- 46:01In our preview, the original number gets
- 46:03rendered. Despite being spelled the
- 46:06same, PHP interprets these definitions
- 46:08as completely different variables. A
- 46:11single typo like this can cause issues.
- 46:14Worst of all, PHP does not throw errors
- 46:16for simple variable typos. If you ever
- 46:20run into a problem where your program is
- 46:22not behaving as expected, the problem
- 46:24may have to do with a typo. Typos are
- 46:26more of a common problem than you think.
- 46:29So, just be aware of that when working
- 46:31with variables. I'm going to remove the
- 46:33second age variable. We don't need it
- 46:36anymore. After removing that variable,
- 46:38let's get into data types.
- 46:41[Music]
- 46:51In this lecture, we're going to briefly
- 46:53discuss data types. Applications can
- 46:56store various types of data. Variables
- 46:58are the most common solution for storing
- 47:01data. For the best performance possible,
- 47:03programming languages categorize data.
- 47:06Categories for data are known as data
- 47:08types. For example, if you're working
- 47:10with numbers, the data type will be an
- 47:13integer. If you're working with a file,
- 47:15the data type should be for a file. So
- 47:17on and so forth. But why why is it
- 47:20important to categorize our data?
- 47:23Categorizing data allows programming
- 47:25languages to optimize your applications.
- 47:27If a variable stores a number, the
- 47:29program never has to worry about working
- 47:32with dates or random text. In addition,
- 47:35it provides clarity as to the type of
- 47:37data you're working with, which can be
- 47:39helpful with debugging an application.
- 47:42Data types are important in PHP because
- 47:45they determine the type of data that a
- 47:47variable can store and how it can be
- 47:49used in your code. By specifying the
- 47:52data type of a variable, you can ensure
- 47:54that it can only be used in a certain
- 47:57way and prevent unexpected errors from
- 47:59occurring when the code is executed. For
- 48:02example, consider a scenario where you
- 48:04need to calculate the average of a set
- 48:06of numbers. If you store these numbers
- 48:08with the incorrect data type, you will
- 48:11not be able to perform mathematical
- 48:13operations on them. Any calculations
- 48:15would produce inaccurate results.
- 48:17However, if you store these numbers
- 48:19correctly, you can perform the
- 48:21calculation as expected. Here's an
- 48:24analogy to help understand the
- 48:26importance of data types. Imagine you
- 48:28are cooking a meal and need to measure
- 48:30ingredients. If you were to use a cup to
- 48:33measure salt instead of a teaspoon, the
- 48:35amount you would use would be
- 48:37significantly different and the meal
- 48:39would not turn out as expected. This is
- 48:41similar to how using the wrong data type
- 48:43in your code can result in unexpected
- 48:45results. By specifying the correct data
- 48:48type, you can ensure that your code
- 48:50produces the correct results, thus
- 48:52preventing bugs and errors from
- 48:54occurring. With that being said,
- 48:56programming languages tackle data types
- 48:58differently. The concept of a data type
- 49:01is not unique to PHP. Most modern
- 49:04programming languages have data types.
- 49:06Generally speaking, there are two types
- 49:08of languages, which are statically typed
- 49:11languages and dynamically typed
- 49:13languages. Statically typed languages
- 49:16require developers to be explicit with
- 49:18their data types. A variable cannot be
- 49:21declared without a type. After setting
- 49:23the type, the variable's type may never
- 49:25change for the lifetime of the program.
- 49:28In contrast, dynamically typed languages
- 49:31do not require developers to set the
- 49:33type. The programming language can
- 49:36automatically determine the type for
- 49:38you. Flexibility is another component of
- 49:40dynamically typed languages. We're
- 49:43allowed to change the type at any time.
- 49:46For example, initially we may want to
- 49:48store a number in a variable. In the
- 49:51future, we have the option of storing a
- 49:53file in the same variable. This action
- 49:56will be acceptable under a dynamically
- 49:58typed language. So where does PHP land?
- 50:02PHP is a dynamically typed language.
- 50:05Dynamically typed languages are
- 50:07considered to be beginnerfriendly. It's
- 50:09one less step you must take to write an
- 50:11application. Unfortunately, there are
- 50:13downsides to dynamically typed
- 50:15languages. First and foremost, they can
- 50:18be slightly slower than statically typed
- 50:21languages. By shifting the
- 50:22responsibility of assigning a type to
- 50:24the programming language, languages must
- 50:27work harder to categorize your data.
- 50:29Secondly, you're not guaranteed a
- 50:31variable may or may the same type for
- 50:33the lifetime of a program. Suddenly, a
- 50:36variable can change from being a number
- 50:38to a string. Inconsistencies can cause a
- 50:41program to behave unexpectedly.
- 50:43Fortunately, PHP has grown to address
- 50:46the shortcomings of a dynamically typed
- 50:48language. As we progress through the
- 50:50course, we'll come across proper
- 50:52debugging techniques and best practices
- 50:55to avoid errors. First, let's start by
- 50:58exploring the data types available from
- 51:00PHP. In total, PHP has nine data types.
- 51:04They're called null, boolean, integer,
- 51:07float, string, array, object, callable,
- 51:11and resource. We're not going to be able
- 51:14to explore all data types as some of
- 51:16them are for advanced use cases. Let's
- 51:19focus on the simpler data types. In the
- 51:22next set of lectures, let's try viewing
- 51:24the data type of a variable.
- 51:26[Music]
- 51:36In this lecture, we're going to start
- 51:38using functions. Functions are another
- 51:41commonly used feature of PHP. So what is
- 51:43a function? Functions are a feature for
- 51:46performing a specific set of
- 51:48instructions. Hold on a moment. Isn't
- 51:50that the same as keywords? As we know,
- 51:53keywords are also used for giving the
- 51:55machine instructions to perform. So what
- 51:58are the differences? There are actually
- 52:00a lot of differences, but for the sake
- 52:03of simplicity, I'm going to summarize it
- 52:05into one key difference. Keywords are
- 52:08considered to be a core feature of the
- 52:09PHP language. We can't disable or remove
- 52:12keywords from PHP. On the other hand,
- 52:15functions can be disabled from the
- 52:17language. But why would someone want to
- 52:19do that? PHP has a few questionable
- 52:22functions in its repertoire. There are
- 52:24developers who believe these functions
- 52:26should be cautiously used by the
- 52:28community. To prevent developers from
- 52:30accidentally using a dangerous function,
- 52:32it's advisable to disable them.
- 52:36Keywords can't be disabled, whereas
- 52:38functions can be disabled. We're not
- 52:40going to need to disable any function
- 52:42for this course. However, it is
- 52:44something you should be aware of when
- 52:46dealing with functions. In the resource
- 52:48section of this lecture, I provide a
- 52:51link to an official list of functions
- 52:53available from PHP. Almost immediately,
- 52:56you're going to notice another
- 52:57difference between keywords and
- 52:59functions. PHP has hundreds of
- 53:01functions, whereas there are only a
- 53:04handful of keywords. Scrolling through
- 53:06this page, you'll find a function for
- 53:08just about anything. You can perform
- 53:10actions from reading a file to
- 53:12encrypting data. I know it can feel
- 53:15overwhelming at first, but don't feel
- 53:17stressed about the enormous list of
- 53:19functions. It's highly unlikely you're
- 53:22ever going to need all of them. I myself
- 53:24haven't even used more than 20% of the
- 53:27functions listed on this page. So, what
- 53:30functions are we going to use? For
- 53:32starters, we're going to use a function
- 53:34called variable dump. Let's head over to
- 53:36our project. Inside the script, I'm
- 53:39going to replace the existing code with
- 53:41a function called variable dump.
- 53:46Functions are easy to write. A function
- 53:48can be executed by typing the name of
- 53:50the function followed by a pair of
- 53:52parenthesis. But what are the
- 53:54parentheses for, you may ask? Well, we
- 53:57can pass on data to the function. The
- 53:59question is what kind of data? That'll
- 54:02vary from function to function. The job
- 54:05of the variable dump function is to
- 54:07output a value onto the screen. It's
- 54:10very similar to the echo keyword.
- 54:12However, there's one critical
- 54:13difference. The variable dump function
- 54:16also outputs the data type of the value.
- 54:19For example, let's type 29 into the
- 54:22parenthesis.
- 54:25This time the page displays a value but
- 54:28the data type as well. The variable dump
- 54:30function was introduced to help
- 54:32developers debug their programs. It can
- 54:35be useful to view the data type of your
- 54:37values. In the next set of lectures,
- 54:40we're going to start using this function
- 54:42to help us inspect the various data
- 54:44types offered by the PHP language.
- 54:48[Music]
- 54:58In this lecture, we're going to explore
- 55:00our first data type called null. The
- 55:03null data type represents nothing. If
- 55:05your variable does not store a value,
- 55:08it's considered to be null. Seems
- 55:10strange, but it's not uncommon to have
- 55:12variables that store nothing. In the
- 55:14real world, you may have a mailbox for
- 55:16storing your mail. However, your mailbox
- 55:19is not always full. From time to time,
- 55:21it'll be empty. Just because it's empty
- 55:24doesn't mean you should get rid of your
- 55:25mailbox. Eventually, you're going to
- 55:27receive mail. This same idea can apply
- 55:30to programming. For example, you may
- 55:33want to create a login form for users.
- 55:35It's common for login forms to require a
- 55:38user's email. This information would be
- 55:40stored in a variable. Initially, the
- 55:42form may not be filled in. Therefore,
- 55:45you may have a variable in your
- 55:46application without a value. Let's look
- 55:49at an example by creating a variable
- 55:51called data. I know what you're
- 55:53thinking. Didn't I say earlier that our
- 55:56variable names should be descriptive?
- 55:58That's correct. But for this example,
- 56:00the data variable is going to store
- 56:02various types of data. It's going to be
- 56:05a generic variable for experimenting
- 56:07with data types. After creating the
- 56:10variable, set the value to null.
- 56:14And that's it. We've created a variable
- 56:17that doesn't store anything. Let's
- 56:19replace the number inside the variable
- 56:22dump function with the data variable.
- 56:26After doing so, the value gets rendered
- 56:29on the screen. Now, you may be thinking,
- 56:31why should we take the time to set the
- 56:33variable to null? If it doesn't contain
- 56:35a value, is there a point in defining
- 56:38one? The answer to that question is yes.
- 56:41Let me show you why. In our script,
- 56:43apply a comment to the data variable.
- 56:48There are two things worth noting about
- 56:50the output. Firstly, PHP throws a
- 56:53warning at us. The warning states the
- 56:55following. Undefined variable data.
- 56:58Undefined warnings are one of the most
- 57:00common errors to receive in PHP. The
- 57:02interpreter is trying to tell us that
- 57:04we're trying to use a variable that
- 57:06doesn't exist in our program. Makes
- 57:08sense, right? PHP can't use a variable
- 57:12that hasn't been created in the first
- 57:14place. The second noteworthy thing is
- 57:16the text after the warning. The value
- 57:19null gets outputed by default. If PHP
- 57:22can't find a variable, it'll fall back
- 57:24to null. Undefined variables are
- 57:27considered to be null. While it's great
- 57:29that undefined variables have a default
- 57:32value, the warning should be avoided. If
- 57:34we were to explicitly create the
- 57:36variable with an initial value of null,
- 57:39we can avoid the warning altogether. I'm
- 57:41going to uncomment the data variable.
- 57:46There's one more thing I want to mention
- 57:48about the null data type. The null
- 57:50keyword is case insensitive. Most
- 57:53developers prefer to use lowercase
- 57:55letters. From time to time, you may come
- 57:57across a different format that uses all
- 58:00uppercase letters like so.
- 58:04There isn't a difference between the
- 58:05spellings. Both formatting solutions
- 58:08result in the same thing. Personally, I
- 58:10prefer to use lowercase letters. All
- 58:13right, that's about it for the null data
- 58:15type. In the next lecture, let's
- 58:17continue exploring other data types.
- 58:20[Music]
- 58:30In this lecture, we're going to explore
- 58:33another data type called boolean.
- 58:35Booleans are a data type for storing
- 58:37truthy or falsy values. That may seem
- 58:40strange. Why would we want to use this
- 58:42data type? It can be useful for
- 58:44determining whether an action can be
- 58:46performed. For example, let's say
- 58:49visitors must be logged in to perform a
- 58:51purchase. If a visitor is not logged in,
- 58:54they will not be able to check out.
- 58:56Storing the user's currently
- 58:57authenticated status as a variable is a
- 59:00common task. Using the boolean type for
- 59:03this scenario would be perfect. In fact,
- 59:05let's give that a try. In the script,
- 59:08define a variable below the data
- 59:10variable called is logged in.
- 59:15Set the value to true.
- 59:19Boolean values can either be true or
- 59:22false. The names are case insensitive.
- 59:25Sometimes developers will use all
- 59:27uppercase letters. There isn't a
- 59:29difference in the formatting. It all
- 59:31boils down to personal preference. For
- 59:33this course, I'll be using lowercase
- 59:35letters. There's something worth
- 59:37pointing out about the name. The name of
- 59:39the variable is a question. For
- 59:41variables that store boolean values,
- 59:44it's common practice to format the name
- 59:46of the variable in the form of a
- 59:48question. For example, let's say we were
- 59:50displaying a list of blog posts. If the
- 59:53user has not written any blog posts, we
- 59:56should display a message to inform them
- 59:58of the absence of blog posts. Storing
- 1:00:01this information as a variable would
- 1:00:03make sense. In this scenario, the
- 1:00:05variable could be called has blog posts.
- 1:00:10Throughout the course, we'll try our
- 1:00:12best to follow this practice. It's not
- 1:00:15required, but recommended. You're going
- 1:00:17to see it all the time in your
- 1:00:18programming career. Let's update the
- 1:00:21value passed into the variable dump
- 1:00:23function to the is logged in variable.
- 1:00:28In the preview, the data type is boolean
- 1:00:32or bool for short. Wrapped inside
- 1:00:34parentheses is the value. For
- 1:00:36demonstration purposes, I'll update the
- 1:00:39variable to false.
- 1:00:42As you can see, the value has been
- 1:00:44updated to reflect the new value.
- 1:00:47Booleans are one of the most memory
- 1:00:49efficient data types. They're perfect
- 1:00:51for scenarios that require a yes or no
- 1:00:53answer. In the next lecture, let's start
- 1:00:56exploring data types for numbers.
- 1:00:58[Music]
- 1:01:09In this lecture, we're going to start
- 1:01:11exploring two data types for numbers
- 1:01:13called integers and floats. Initially,
- 1:01:16that may seem strange. Why are there two
- 1:01:19data types for numbers? The reason has
- 1:01:21to do with memory efficiency. Storing
- 1:01:24numbers with decimal values requires
- 1:01:26additional work. They're not easy to
- 1:01:28store. Therefore, it's efficient to have
- 1:01:31a data type dedicated to whole numbers
- 1:01:34and another data type for numbers with
- 1:01:36decimal values. Most of the complexities
- 1:01:39are handled for you. It's not something
- 1:01:41you'll have to worry about. Regardless,
- 1:01:43it can be beneficial to know the
- 1:01:45differences. Let's start with integers.
- 1:01:48The integer data type was designed for
- 1:01:50storing whole numbers in our script.
- 1:01:53Let's set the data variable to a random
- 1:01:56whole number.
- 1:01:59Next, let's update the variable dump
- 1:02:02function to output the data variable.
- 1:02:06As you can see, the data type has been
- 1:02:09set to integer. PHP supports negative
- 1:02:12numbers, too. For example, let's add the
- 1:02:15negative symbol before the number.
- 1:02:19Despite adding this symbol, the value
- 1:02:22continues to be an integer. What if
- 1:02:24we're dealing with large numbers? For
- 1:02:26example, I'm going to set the value to 5
- 1:02:28million.
- 1:02:31If you're like me, you may have
- 1:02:33difficulty reading this number.
- 1:02:34Unfortunately, PHP does not support
- 1:02:37commas in our numbers. Instead of using
- 1:02:40commas, we can use underscore
- 1:02:42characters. Underscore characters can be
- 1:02:44inserted anywhere inside the number.
- 1:02:46It's common practice to insert these
- 1:02:48characters for every three numbers. I'm
- 1:02:51going to quickly add them in.
- 1:02:55These characters don't affect the
- 1:02:56number. PHP removes them during runtime.
- 1:03:00The purpose of these characters was to
- 1:03:02help with readability for large numbers.
- 1:03:04They are completely optional. If you can
- 1:03:06read large numbers without these
- 1:03:08characters, don't feel pressured to add
- 1:03:10them. After updating the script, the
- 1:03:12large number appears normally in the
- 1:03:14preview. PHP strips away the underscore
- 1:03:18characters from the numbers. we never
- 1:03:20have to worry about them affecting our
- 1:03:22program. Let's shift our focus to
- 1:03:25floats. A float is the data type for
- 1:03:28numbers with decimals. Similar to
- 1:03:30integers, floats support negative
- 1:03:32numbers and underscore characters for
- 1:03:35large numbers. Let's create our first
- 1:03:37float. Set the data variable to the
- 1:03:40following value 1 2 3.45.
- 1:03:46You may be wondering why is it called a
- 1:03:48float? The reason has to do with the
- 1:03:50placement of the decimal separator. The
- 1:03:53dot can be positioned anywhere in the
- 1:03:55number. We can position it at the
- 1:03:57beginning of the number. Alternatively,
- 1:03:59we can position it between the second
- 1:04:01and third numbers. You can think of it
- 1:04:03as a character that can float anywhere
- 1:04:06around the entirety of the number. If
- 1:04:08you think that's strange, I would agree
- 1:04:11with you. There's a joke among the
- 1:04:12community that programmers are terrible
- 1:04:14at naming things. It would have been
- 1:04:16simpler to use the word decimal as the
- 1:04:19name for the type. Regardless, it's been
- 1:04:21established that numbers with decimal
- 1:04:24values are considered to be floats. In
- 1:04:26the preview, the data type has been
- 1:04:28marked as a float. Perfect. Integers and
- 1:04:31floats are two data types for storing
- 1:04:34numbers. However, PHP is doing us a
- 1:04:36favor by optimizing the memory for our
- 1:04:39program. Numbers with decimals require
- 1:04:42more memory than numbers without decimal
- 1:04:44values. Overall, we benefit from this
- 1:04:47behavior. In the next lecture, let's
- 1:04:49continue exploring data types.
- 1:04:52[Music]
- 1:05:02In this lecture, we're going to explore
- 1:05:05another data type called strings.
- 1:05:07Strings are the data type for storing
- 1:05:09text. Sounds like a strange name. Why
- 1:05:12would developers call it a string? Text
- 1:05:14is a combination of multiple characters
- 1:05:16that are strung together, hence the name
- 1:05:19string. It's a weird naming convention,
- 1:05:21but that's what the community has
- 1:05:23settled on. Regardless, if you're
- 1:05:25working with text, the data type is
- 1:05:27considered to be a string. Let's try
- 1:05:30creating a string. For the data
- 1:05:32variable, let's replace the existing
- 1:05:34value with the following value. John
- 1:05:39text must be wrapped with quotes. Single
- 1:05:42quotes and double quotes are completely
- 1:05:44valid. In this example, I'm using single
- 1:05:47quotes. For demonstration purposes, I'll
- 1:05:50use double quotes for a different
- 1:05:52variable. Below, create another variable
- 1:05:55called last name with a value of Smith.
- 1:06:01This time I'm using double quotes.
- 1:06:03Inside the quotes, we're allowed to
- 1:06:05write anything we'd like. blog posts,
- 1:06:08product names, addresses, or anything
- 1:06:11represented as text can be stored in a
- 1:06:13string. It's important to note that code
- 1:06:16inside a string won't be executed. For
- 1:06:19example, inside the data variables
- 1:06:22value, let's call the variable dump
- 1:06:24function.
- 1:06:27As you can see, the contents of the
- 1:06:29value do not get executed. PHP does not
- 1:06:32run the variable dump function from
- 1:06:34within the string. It's interpreted as
- 1:06:37plain text. If we shift our focus to the
- 1:06:39data type, it's been recognized as a
- 1:06:42string. Looking closely at the output,
- 1:06:45PHP has taken the time to count the
- 1:06:47number of characters from within the
- 1:06:49string. Overall, strings are simple to
- 1:06:52create. As long as you're wrapping text
- 1:06:54with quotes, you're good to go. The
- 1:06:56question is, what's the difference
- 1:06:58between single quotes and double quotes?
- 1:07:01Most developers prefer single quotes for
- 1:07:04readability. There's less clutter.
- 1:07:06However, double quotes offer one
- 1:07:08advantage over single quotes.
- 1:07:12PHP supports string interpolation. I
- 1:07:15know that looks like a scary,
- 1:07:16complicated word, but I promise it's
- 1:07:19not. String interpolation is the process
- 1:07:21of replacing a placeholder with a value.
- 1:07:24It's as simple as that. For example,
- 1:07:27let's say we have the following
- 1:07:28sentence. Hello, my name is X. The
- 1:07:31letter X is a placeholder. We can swap
- 1:07:34this placeholder with an actual value.
- 1:07:37String interpolation is a common feature
- 1:07:39you'll be using in your programs.
- 1:07:43This feature is only available with
- 1:07:45strings that are wrapped with double
- 1:07:47quotes. Inside the last name variable,
- 1:07:50let's inject the data variable by adding
- 1:07:53it at the beginning of the string.
- 1:07:57We're allowed to inject variables into a
- 1:08:00string. PHP replaces this portion of the
- 1:08:03string with the value stored in the data
- 1:08:05variable. Before testing our work, let's
- 1:08:08remove the variable dump function from
- 1:08:11the data variable.
- 1:08:14Next, update the variable dump function
- 1:08:17to output the last name variable.
- 1:08:22The entire name has been rendered in the
- 1:08:24preview. String interpolation is a
- 1:08:26simple yet handy feature. There's one
- 1:08:29thing I should mention. In some cases,
- 1:08:31you may want to append text immediately
- 1:08:34after a variable. For example, in the
- 1:08:37last name variable, I'll add the
- 1:08:39trademark symbol after the data
- 1:08:41variable.
- 1:08:44Adding this symbol produces an error.
- 1:08:46The reason is simple. PHP assumes that
- 1:08:49the trademark symbol is part of the
- 1:08:51variable name. It'll attempt to search
- 1:08:54for a variable with this name, which
- 1:08:56doesn't exist. So, how do we output the
- 1:08:59variable while keeping the symbol? We
- 1:09:01can wrap the variable with curly
- 1:09:03brackets like so.
- 1:09:07Curly brackets are optional. However, if
- 1:09:09they're added, PHP ignores text written
- 1:09:12outside of it. You don't have to worry
- 1:09:15about the curly brackets appearing
- 1:09:16within the string. PHP removes them
- 1:09:19during output. In the output, the full
- 1:09:21name gets outputed. Perfect. There's one
- 1:09:24more thing I want to show you before
- 1:09:26ending the lecture. So far, we've been
- 1:09:29rendering the entire string. In some
- 1:09:31cases, you may want to output a specific
- 1:09:34character. It turns out that PHP allows
- 1:09:37us to access specific characters from a
- 1:09:39string. In the variable dump function,
- 1:09:42let's add a pair of square brackets.
- 1:09:46If we're dealing with a string, we can
- 1:09:49access a specific character by adding
- 1:09:51these brackets. Within these brackets,
- 1:09:53we must add a number. Behind the scenes,
- 1:09:56PHP indexes our string. It'll assign a
- 1:09:59number to each character. The first
- 1:10:01character is assigned an index of zero.
- 1:10:04The second character is assigned an
- 1:10:06index of one. So on and so forth. As an
- 1:10:09exercise, what number do you think is
- 1:10:11assigned to the fourth character? I'll
- 1:10:14give you a moment to think about this.
- 1:10:18The answer is three. Let's try testing
- 1:10:21our theory. In the square brackets, type
- 1:10:23the number three.
- 1:10:27Cool. As you can see, the fourth
- 1:10:29character gets outputed. It might seem
- 1:10:31strange. Programming languages commonly
- 1:10:34count at zero instead of one. It's a
- 1:10:36strange habit you'll have to get used
- 1:10:38to. Don't worry. As we progress through
- 1:10:41the course, counting from zero will
- 1:10:43become natural. We're not limited to
- 1:10:45accessing a character. We can also
- 1:10:48update a specific character from a
- 1:10:50string. Above the variable dump
- 1:10:52function, let's set the last name three
- 1:10:55variable to X.
- 1:10:59Once again, I'm using the square bracket
- 1:11:02syntax. This time we're directly
- 1:11:04updating this character. PHP won't
- 1:11:07update the entire string. The fourth
- 1:11:09character has been changed to X. There's
- 1:11:12one thing worth highlighting. We're
- 1:11:14updating the character before running
- 1:11:16the function. The order of our code does
- 1:11:19matter. PHP executes code from top to
- 1:11:22bottom. If we updated the character
- 1:11:24after calling the variable dump
- 1:11:26function, the function would output the
- 1:11:28original value of the string. Not taking
- 1:11:30the time to arrange your code can result
- 1:11:33in unexpected behavior. I think it's
- 1:11:36time for an exercise. In the next
- 1:11:38lecture, let's tackle an exercise to
- 1:11:40reinforce what we've learned about data
- 1:11:42types.
- 1:11:44[Music]
- 1:12:04In this lecture, we're going to explore
- 1:12:07another data type called arrays. Arrays
- 1:12:10were designed for storing a collection
- 1:12:12of values. For example, let's say we
- 1:12:15were hired to develop a food tracking
- 1:12:17application. Users may want to store a
- 1:12:19list of their favorite foods. One
- 1:12:22solution would be to store the foods in
- 1:12:24a variable. In our script, let's create
- 1:12:26a variable called food one with a value
- 1:12:29of salad.
- 1:12:33Afterward, I'll create another variable
- 1:12:35called food two with a value of burger.
- 1:12:41I can continuously create variables for
- 1:12:43each of my favorite foods. However, this
- 1:12:46process can be cumbersome. Creating a
- 1:12:49unique variable for each food item is
- 1:12:51not scalable. What if we need to remove
- 1:12:53an item from our list? What if we need
- 1:12:56to add more foods or replace existing
- 1:12:58foods? Managing the variable names is
- 1:13:01tedious as the list grows. Arrays can
- 1:13:04simplify the process. PHP allows
- 1:13:07developers to store a list of values
- 1:13:09from within a single variable. Let's try
- 1:13:12creating an array by completely removing
- 1:13:14these variables except the data
- 1:13:16variable. Next, let's update the data
- 1:13:20variables value to a pair of square
- 1:13:22brackets.
- 1:13:25Arrays are created with a pair of square
- 1:13:28brackets. Inside these brackets, we can
- 1:13:30start writing a list of values. Strings,
- 1:13:33booleans, integers, and floats can be
- 1:13:36inserted within an array. In addition,
- 1:13:39arrays are not constrained to a single
- 1:13:41data type. We can insert a number
- 1:13:44followed by a string. Let's add our list
- 1:13:47of favorite foods as strings. To
- 1:13:49separate each item in an array, we can
- 1:13:52use a comma.
- 1:13:55I've added three items to my array. Feel
- 1:13:58free to use different values. After
- 1:14:00adding our values, we can access a
- 1:14:03specific value using square brackets.
- 1:14:06Below the variable, update the variable
- 1:14:09dump function with the following value,
- 1:14:11data 1.
- 1:14:15You're already familiar with the syntax.
- 1:14:17Items in arrays can be accessed via
- 1:14:19their index. Behind the scenes, PHP
- 1:14:22assigns a number to each item in our
- 1:14:24array. As mentioned before, PHP starts
- 1:14:28counting at zero. Therefore, the first
- 1:14:30item in the array has an index of zero.
- 1:14:33The second item has an index of one. So
- 1:14:36on and so forth. In this example, we're
- 1:14:39accessing the second item from the
- 1:14:41array. As you can see, the second item
- 1:14:43has appeared as the output. What if we
- 1:14:46want to view the entire array? In that
- 1:14:48case, we can omit the square brackets
- 1:14:50from the variable dump function like so.
- 1:14:56This time the entire array gets
- 1:14:58rendered. There are a few things worth
- 1:15:00mentioning. Firstly, the number of items
- 1:15:03in the array is rendered from within the
- 1:15:05parenthesis. Secondly, the data types
- 1:15:08for the items in the array are
- 1:15:10presented. Lastly, the index of each
- 1:15:13item starts from zero and increments by
- 1:15:15one for each item in the array. We're
- 1:15:18not limited to creating an array.
- 1:15:20Sometimes you may want to update items.
- 1:15:23Specific items can be updated through
- 1:15:25the square bracket syntax. Back in our
- 1:15:28script, let's update the second item in
- 1:15:31the array. Type the following with me.
- 1:15:33Data 1 equals chicken.
- 1:15:38Once again, we're accessing a specific
- 1:15:40item from an array. This time, we're
- 1:15:43updating the value that has been
- 1:15:44assigned an index of one. Before testing
- 1:15:47our code, there's one more thing I want
- 1:15:49to show you. New items can be added to
- 1:15:52an array by writing a pair of empty
- 1:15:54square brackets. For demonstration
- 1:15:56purposes, let's add a new item to our
- 1:15:59array.
- 1:16:02By not accessing a specific index, PHP
- 1:16:05adds the value at the end of the array.
- 1:16:08Looking at the output, the new item has
- 1:16:10an index of three. In addition, the
- 1:16:13array size has changed from three to
- 1:16:15four. Adding a new item to the array has
- 1:16:18increased its size. PHP was able to
- 1:16:22successfully add a new item to our
- 1:16:24array. Overall, arrays can be a powerful
- 1:16:27feature for storing a collection or list
- 1:16:30of data. If you think about it, most
- 1:16:32applications deal with lists. A list of
- 1:16:35users, posts, settings, and images are
- 1:16:38examples of data that can be stored in
- 1:16:40an array. Arrays have so much more to
- 1:16:43offer. In the next lecture, let's
- 1:16:45continue exploring other features of
- 1:16:47arrays.
- 1:16:49[Music]
- 1:16:59In this lecture, we're going to explore
- 1:17:01an optional feature of arrays called
- 1:17:04named keys. As we know, PHP assigns
- 1:17:07numbers to each item in an array. You
- 1:17:10can refer to these numbers as indexes.
- 1:17:13However, there's another name you can
- 1:17:15use, which is keys. Regardless of what
- 1:17:17you call it, we're not forced to use the
- 1:17:20number system. In some cases, we may
- 1:17:23want to add a name to describe the value
- 1:17:25in an array. Names are considered to be
- 1:17:28more memorable. In addition, they can be
- 1:17:30useful for better describing the data
- 1:17:32stored in an array. PHP supports this
- 1:17:35feature, which is referred to as an
- 1:17:37associative array. Associative arrays
- 1:17:40use names for keys instead of numbers.
- 1:17:43Let's look at an example. In our array,
- 1:17:46we're storing a list of food. What if we
- 1:17:48want to associate each food with a
- 1:17:50specific user? It could be presented as
- 1:17:53a user's favorite food. Before the first
- 1:17:55value, add the following. John equals
- 1:17:58greater than.
- 1:18:02A named key can be assigned by adding
- 1:18:04the double arrow operator. To the left
- 1:18:07of this operator, we can assign a name
- 1:18:09to our key. In this case, we're using
- 1:18:11the name John. to the right of this
- 1:18:14operator. We must specify the value to
- 1:18:17associate with the key by adding a name.
- 1:18:20PHP does not add a numeric index to this
- 1:18:23value. Our array outputs the values
- 1:18:26including the value with the named key.
- 1:18:29Looking closely, the key does not hold a
- 1:18:32number. Rather, it holds the name we
- 1:18:34assigned it during the variable
- 1:18:36declaration. It's completely optional to
- 1:18:39add named keys to all values. If we do
- 1:18:42not set a name for a key, PHP
- 1:18:44automatically defaults to a number.
- 1:18:47However, this practice is uncommon.
- 1:18:49Developers will often stick to numbers
- 1:18:51or named keys in a single array. It's
- 1:18:54better to be consistent. For that
- 1:18:56reason, let's update the array by adding
- 1:18:59names to each item in the array. You can
- 1:19:02use whatever names you'd like. I'll
- 1:19:04quickly update the array.
- 1:19:08In the array, the first three items have
- 1:19:10a name. There's just one problem. Two
- 1:19:13additional items have been added. If we
- 1:19:15look back at our code, we're adding two
- 1:19:17items called chicken and tomato soup.
- 1:19:21Previously, we were updating the item
- 1:19:23with an index of one. However, since we
- 1:19:26have an associative array, there isn't
- 1:19:29an item with this index. If a value
- 1:19:31doesn't exist at a specific index, PHP
- 1:19:34creates the index. That's not what we
- 1:19:37want. We should update the item in the
- 1:19:39array with the key called Jane. Luckily,
- 1:19:42that's easy. Inside the brackets,
- 1:19:44replace the number with the key name.
- 1:19:49This update should effectively update an
- 1:19:51existing item. Below this line of code,
- 1:19:54we're adding a new item to the array.
- 1:19:56Currently, we're not assigning a name.
- 1:19:58Therefore, PHP assigns a number. Let's
- 1:20:01add a name in the square brackets.
- 1:20:06After making those changes, all items in
- 1:20:09the array have names. We can reference a
- 1:20:11specific item in the array by a user's
- 1:20:14name. Associative arrays can be helpful
- 1:20:16when dealing with arrays that have
- 1:20:18limited data. Adding names can be a
- 1:20:21great way to describe the data stored at
- 1:20:23a specific index. In the following
- 1:20:26lecture, let's look at one more feature
- 1:20:28related to arrays.
- 1:20:30[Music]
- 1:20:41In this lecture, we're going to explore
- 1:20:43a feature called multi-dimensional
- 1:20:45arrays. Sounds like a complicated
- 1:20:47feature, but I promise it's not. A
- 1:20:50multi-dimensional array is when an array
- 1:20:52stores another array. Thus, you end up
- 1:20:55having a nested array structure. Nested
- 1:20:58arrays are very common in programming.
- 1:21:00Let's look at an example. In our array,
- 1:21:03each user has one favorite food. But
- 1:21:06what if they have multiple? It would
- 1:21:08make sense to use arrays instead of
- 1:21:10strings for storing this information.
- 1:21:13This way, users can have multiple
- 1:21:15favorite foods. For the first item in
- 1:21:17the array, let's wrap the string with
- 1:21:20square brackets.
- 1:21:23Voila, we have a multi-dimensional
- 1:21:26array. It's an array from within an
- 1:21:29array. There's no limit as to how many
- 1:21:31nested arrays can be created. This
- 1:21:33feature can be handy for creating a
- 1:21:36complex structure of data. I'm going to
- 1:21:38add a second item to the array.
- 1:21:43In the preview, our array has four
- 1:21:45items. The first item stores another
- 1:21:48array. The question is, what if we want
- 1:21:51to access a specific item from a nested
- 1:21:54array? That's simple. We can use square
- 1:21:56bracket syntax. Back in the variable
- 1:21:59dump function, add a pair of square
- 1:22:01brackets to access the item called John.
- 1:22:06Let's try accessing the second item in
- 1:22:09the array by adding an additional pair
- 1:22:11of square brackets. Inside these
- 1:22:13brackets, type the number one.
- 1:22:18Look at that. The second item from the
- 1:22:20nested array has been outputed. That's
- 1:22:22exactly what we were looking for. What
- 1:22:24do you think would happen if we tried
- 1:22:26accessing a nested item from an array
- 1:22:29that doesn't exist? Let's find out. In
- 1:22:32the variable dump function, change the
- 1:22:34value from within the first pair of
- 1:22:36square brackets to Bob.
- 1:22:40Currently, we don't have an item in our
- 1:22:43array with a key called Bob. As
- 1:22:45expected, an error gets produced.
- 1:22:48Typically, one error would appear. This
- 1:22:50time, PHP is rendering two errors. Let's
- 1:22:53dissect them. The first error is an
- 1:22:56undefined error. Whenever you see the
- 1:22:58word undefined, it means that a variable
- 1:23:01does not exist. Your script is
- 1:23:03attempting to access a variable that
- 1:23:06hasn't been defined or is inaccessible.
- 1:23:09This error makes complete sense in the
- 1:23:11variable dump function or attempting to
- 1:23:14access an item with a key called Bob,
- 1:23:17which doesn't exist. As for the second
- 1:23:19error, the description states the
- 1:23:21following. trying to access array offset
- 1:23:24on value of type null. The error states
- 1:23:27that we're trying to access an array. If
- 1:23:30we look at our code, we're trying to
- 1:23:32access the second item from an array
- 1:23:34called Bob. However, this array doesn't
- 1:23:37exist. If an array doesn't exist, square
- 1:23:40bracket syntax does not work. PHP states
- 1:23:43that we're trying to use an array that
- 1:23:46doesn't exist. Hopefully, that makes
- 1:23:48sense. It's a possible error you might
- 1:23:50encounter whenever working with
- 1:23:52multi-dimensional arrays. You can't
- 1:23:54access a nested array if an incorrect
- 1:23:57item from the list is selected. That
- 1:23:59wraps up multi-dimensional arrays.
- 1:24:02Actually, that wraps up our discussion
- 1:24:04on the various data types. There are
- 1:24:06other data types worth exploring, but
- 1:24:09we'll get to those in the future. For
- 1:24:11now, I think it's time we start shifting
- 1:24:13our focus to another subject. In the
- 1:24:15next lecture, let's start talking about
- 1:24:17type casting.
- 1:24:19[Music]
- 1:24:29In this lecture, we're going to explore
- 1:24:32type casting. From time to time, you may
- 1:24:34need to change the data type of an
- 1:24:36existing value. It's not uncommon to
- 1:24:39perform this type of action. PHP refers
- 1:24:42to this type of behavior as type
- 1:24:44casting. Typ casting is the process of
- 1:24:47converting the data type of a value to a
- 1:24:49different data type. Overall, PHP makes
- 1:24:52it extremely simple to change data
- 1:24:55types. Type casting can be performed by
- 1:24:57wrapping the new type with parenthesis.
- 1:25:00Afterward, you must provide the value to
- 1:25:03change. In this example, I'm
- 1:25:05demonstrating type casting on a single
- 1:25:07variable. It's cast into a boolean,
- 1:25:10integer, float, string, and an array.
- 1:25:13While type casting is powerful, the
- 1:25:16results can be unexpected.
- 1:25:19In the resource section of this lecture,
- 1:25:21I provide a link to a gist. If you're
- 1:25:24not familiar with GitHub, that's
- 1:25:26perfectly fine. GitHub is a resource for
- 1:25:28sharing code with others. In a future
- 1:25:31lecture, we're going to have an
- 1:25:32opportunity to learn more about GitHub.
- 1:25:35For now, all you need to know is that
- 1:25:37I'll be using it to share code with you.
- 1:25:39To prevent the lecture from becoming
- 1:25:41repetitive, I've decided to provide an
- 1:25:44entire example of type casting of
- 1:25:46various types and values. Copy this
- 1:25:49entire piece of code to your clipboard.
- 1:25:53Next, switch over to our project. Inside
- 1:25:56the index.php file, replace the current
- 1:26:00contents of the file with the code in
- 1:26:02your clipboard. Be sure not to replace
- 1:26:04the opening PHP tags, otherwise this
- 1:26:08code would not work.
- 1:26:11All right, let's take the time to
- 1:26:13understand what's going on. If we look
- 1:26:15through the code, we are repeatedly
- 1:26:17calling the variable dump function. The
- 1:26:20main difference is the value passed into
- 1:26:22the function. Various values are being
- 1:26:25type cast. Let's start from the top and
- 1:26:27work our way down. The first set of
- 1:26:30examples is non boolean values being
- 1:26:33casted into boolean values. We can use
- 1:26:35the bool keyword to perform this
- 1:26:37operation. Alternatively, we can type
- 1:26:40boolean as an example. The second
- 1:26:43variable dump function uses the boolean
- 1:26:46keyword instead of the bool keyword. I
- 1:26:48prefer to use the shorter version. Let's
- 1:26:51go through these examples one by one.
- 1:26:53Strings can be typecasted into booleans.
- 1:26:56However, that doesn't mean all strings
- 1:26:59are converted to the same value. Empty
- 1:27:01strings are considered to be false. If
- 1:27:04the string contains text, the value
- 1:27:06becomes true. The exception to this rule
- 1:27:09is the third example, which is a string
- 1:27:11with the number zero. On the other hand,
- 1:27:14if the text says false, the value is
- 1:27:17casted into true. Moving on to integers
- 1:27:20and floats, numbers are set to true when
- 1:27:23the number is not zero. This includes
- 1:27:25negative numbers too. If the number is
- 1:27:28zero, the value becomes false. Even
- 1:27:31though these examples are integers, the
- 1:27:33same rules apply to floats. Up next,
- 1:27:36null and empty arrays are considered to
- 1:27:39be false. On the other hand, if the
- 1:27:41array contains one or more items, the
- 1:27:44value becomes true. It doesn't matter
- 1:27:47what the contents of the array are. As
- 1:27:49long as the array has values, the array
- 1:27:51is converted into true. You can confirm
- 1:27:54these values in the preview. They should
- 1:27:57match the values written in the
- 1:27:59comments. If it's hard to read these
- 1:28:01values, feel free to add break lines
- 1:28:03into the output using HTML. Let's move
- 1:28:07on to type casting values to integers.
- 1:28:10Just so that the preview doesn't get
- 1:28:12cluttered with a bunch of values, I've
- 1:28:14applied comments to the other examples.
- 1:28:16I'm going to comment the boolean
- 1:28:18examples and uncomment the integer
- 1:28:21examples. Here's a cool trick to
- 1:28:23instantly apply or remove comments from
- 1:28:26multiple lines of code. If you were to
- 1:28:28select multiple lines of code and then
- 1:28:30press the keyboard shortcut control
- 1:28:34slash, comments are toggled on the
- 1:28:36selected lines. For Mac users, this
- 1:28:39shortcut would be command slash.
- 1:28:44All right. Values can be typecasted to
- 1:28:47integers with the int keyword.
- 1:28:49Alternatively, you can use the integer
- 1:28:52keyword which is shown in the second
- 1:28:54example. However, like the boolean
- 1:28:56example, I prefer the shorter version.
- 1:28:59In the first few examples, boolean
- 1:29:01values are casted into integers. A false
- 1:29:04boolean is set to zero, whereas a true
- 1:29:07boolean value is set to one. If we're
- 1:29:10updating strings, PHP removes
- 1:29:13non-numeric characters from the string.
- 1:29:16For example, a string with a number
- 1:29:18minus one is set to minus1. However, if
- 1:29:21our string doesn't contain numbers, the
- 1:29:23default value is zero. What if we have a
- 1:29:26combination of numbers or characters?
- 1:29:28Non-numeric characters are removed,
- 1:29:31leaving us with the number. If we
- 1:29:33convert a float to an integer, the
- 1:29:35decimal value is wholly removed. This is
- 1:29:38important to understand. PHP does not
- 1:29:41round the value. numbers to the right of
- 1:29:43the dot character are removed. If we
- 1:29:46were to round this value, the result
- 1:29:48would be 13. However, if you look at the
- 1:29:50output, the value is 12. Understanding
- 1:29:53this behavior can be a small gotcha if
- 1:29:56you're not aware of it. Lastly, a null
- 1:29:58value is set to zero. Let's move on to
- 1:30:01the float examples. Similar to before,
- 1:30:04comment the integer examples and
- 1:30:06uncomment the float examples.
- 1:30:10values can be typcasted to a float with
- 1:30:13the float keyword. Most of the
- 1:30:15conversions are similar to integers. If
- 1:30:18I'm being completely honest, there's
- 1:30:20nothing new to share. So, let's just
- 1:30:22move on to the next data type. Comment
- 1:30:25the float examples and then uncomment
- 1:30:27the string examples.
- 1:30:31Type casting to strings can be performed
- 1:30:33with the string keyword. Let's go
- 1:30:36through the examples. Firstly, a false
- 1:30:39value is set to an empty string. On the
- 1:30:41other hand, a true value is set to one.
- 1:30:44Integers and floats are converted to
- 1:30:47strings without stripping numbers from
- 1:30:49the original value. Basically, PHP just
- 1:30:53wraps your numbers with quotes. Arrays
- 1:30:56are a special case. They can be
- 1:30:58converted, but PHP does throw a warning
- 1:31:00during this process. It's not able to
- 1:31:03completely convert an array to a string.
- 1:31:06All you'll get is the word array. Values
- 1:31:09inside the array are entirely removed.
- 1:31:11If you look at the preview, you'll be
- 1:31:13able to see how they're treated in our
- 1:31:15script. The word array appears, but a
- 1:31:18warning is accompanied with the value.
- 1:31:21Just be aware of this behavior when type
- 1:31:23casting arrays. Lastly, a null value is
- 1:31:27converted into an empty string. That's
- 1:31:29about it for strings. Let's move on to
- 1:31:31the last set of examples. comment the
- 1:31:34string examples and uncomment the array
- 1:31:37examples.
- 1:31:40A value can be typecasted into an array
- 1:31:42with the array keyword. Array type
- 1:31:45casting is straightforward. The value is
- 1:31:48always inserted as the first item in the
- 1:31:50array. It doesn't matter if we're
- 1:31:52working with booleans, strings,
- 1:31:54integers, or floats. The one exception
- 1:31:56to this rule is null values. An empty
- 1:31:59array gets produced for null values.
- 1:32:02That's about it. You can look at the
- 1:32:04preview for the final result of typ
- 1:32:06casting these values. Understanding typ
- 1:32:08casting is essential. Believe it or not,
- 1:32:11PHP can automatically type cast your
- 1:32:14variables without your consent. In the
- 1:32:16following lecture, let's discuss this
- 1:32:18feature.
- 1:32:21[Music]
- 1:32:31In this lecture, we're going to look at
- 1:32:33a feature called type juggling. From
- 1:32:35time to time, you may hear this feature
- 1:32:37referred to as type coercion. They mean
- 1:32:39the same thing. So, what is type
- 1:32:41juggling and why should you care about
- 1:32:43it? There are specific situations where
- 1:32:46PHP attempts to change the data type of
- 1:32:48your values without your explicit
- 1:32:50permission. Even if you're not
- 1:32:52performing type casting, PHP may perform
- 1:32:55type casting automatically. This
- 1:32:57behavior is known as type juggling.
- 1:32:59Believe it or not, we've experienced
- 1:33:01type juggling before.
- 1:33:03In the resource section of this course,
- 1:33:05I provide a link to the echo keyword
- 1:33:08documentation page. Under the
- 1:33:10description section, there's a code
- 1:33:11example of the echo keyword. I didn't
- 1:33:14mention this before, but you can wrap
- 1:33:16the value with a pair of parenthesis.
- 1:33:18However, these characters are completely
- 1:33:20optional. Most developers prefer to skip
- 1:33:23them. The most important part of this
- 1:33:25example is the data type of the value.
- 1:33:28Looking closely, the value after the
- 1:33:30echo keyword should be a string. Isn't
- 1:33:32that strange? We've been able to pass in
- 1:33:34other values such as integers and
- 1:33:36floats. So why does PHP state we must
- 1:33:40pass in a string? Do you remember the
- 1:33:42discussion we had on data types? PHP is
- 1:33:45a dynamically typed language which is
- 1:33:47considered to be beginnerfriendly. One
- 1:33:49of the reasons is because of type
- 1:33:51juggling. If we pass in a value with the
- 1:33:54wrong data type, PHP attempts to rectify
- 1:33:56the situation on our behalf by type
- 1:33:58casting the value. In this case,
- 1:34:01non-strings are type casted into
- 1:34:03strings. Let's look at an example by
- 1:34:05heading over to our project. I'm going
- 1:34:07to remove all the code except the
- 1:34:10opening PHP tag.
- 1:34:13Next, I'm going to echo a random number.
- 1:34:18Despite echoing an integer, the preview
- 1:34:20displays the number perfectly without a
- 1:34:22problem. Once again, PHP is
- 1:34:24automatically type casting the value,
- 1:34:26which is known as type juggling or type
- 1:34:29coercion. It's the same as writing echo
- 1:34:32string.
- 1:34:34At times, this feature can feel
- 1:34:36convenient. It means less code to write.
- 1:34:39On the other hand, it can cause
- 1:34:41unexpected results in your codebase.
- 1:34:43Throughout the course, I'll be sure to
- 1:34:45point out when type juggling can be a
- 1:34:47nuisance. For now, just be aware of this
- 1:34:49feature when working with data. In the
- 1:34:51next lecture, let's shift our focus to
- 1:34:54operators.
- 1:34:56[Music]
- 1:35:06In this lecture, we're going to explore
- 1:35:08arithmetic operators for numbers. In the
- 1:35:11resource section of this lecture, I
- 1:35:13provide a link to a documentation page
- 1:35:15on arithmetic operators. PHP supports
- 1:35:18basic mathematical operations. Addition,
- 1:35:21subtraction, multiplication, and
- 1:35:23division can be performed with PHP. This
- 1:35:26documentation page displays a complete
- 1:35:29list of operators for performing these
- 1:35:31operations. Let's give them a try by
- 1:35:33heading over to our project. I'm going
- 1:35:36to replace the echo statement with a
- 1:35:38variable called data. It'll hold the
- 1:35:40following value 1 + 2 - 3 asterisk 4/5.
- 1:35:49In this example, I'm using some of the
- 1:35:51arithmetic operators. The other
- 1:35:53operators will be introduced in a
- 1:35:55moment. Most of the operators are
- 1:35:57self-explanatory. The plus character
- 1:36:00performs addition. The minus character
- 1:36:02performs subtraction. The asterisk
- 1:36:04character performs multiplication. And
- 1:36:07lastly, the slash character performs
- 1:36:10division. Similar to math, the order of
- 1:36:12operations is applicable to our
- 1:36:14equation. PHP performs multiplication
- 1:36:17followed by division, addition, and
- 1:36:20subtraction. The result will be stored
- 1:36:22in the variable. This is important to
- 1:36:24understand. PHP always executes
- 1:36:27mathematical operations before assigning
- 1:36:29a value to a variable. Take a moment to
- 1:36:32figure out the value that will be
- 1:36:34computed by this equation. Let's log the
- 1:36:36value with the variable dump function.
- 1:36:41As you can see, the data type has been
- 1:36:43set to float with a value of 0.6. The
- 1:36:47value is abnormal. Technically, the
- 1:36:49result should be 0.6. There shouldn't be
- 1:36:52a chain of zeros after the number. So,
- 1:36:54why are we receiving an abnormal value?
- 1:36:59First, let's revisit the concept of
- 1:37:02machine code. Machine code is a
- 1:37:04low-level language understood by
- 1:37:06machines. Everything is written with
- 1:37:08zeros and ones. As you can probably
- 1:37:10guess, machine code can be a nightmare
- 1:37:13to read and write. While it's possible
- 1:37:15to create applications with machine
- 1:37:17code, it would take a lot of work to
- 1:37:19create a simple app. For this reason,
- 1:37:21programming languages were introduced.
- 1:37:23Readability is the goal of most
- 1:37:25languages on the web. At the end of the
- 1:37:27day, our computers can't understand any
- 1:37:29programming language except machine
- 1:37:31code. It doesn't matter if you're using
- 1:37:33PHP, C++, or JavaScript. All programming
- 1:37:37languages cannot be understood by a
- 1:37:39machine. Converting a language to
- 1:37:41another language is known as
- 1:37:42compilation. Programming languages can
- 1:37:45use a compiler or interpreter to perform
- 1:37:47this process. In the case of PHP, an
- 1:37:50interpreter is used to perform this
- 1:37:52process. While there are differences
- 1:37:55between a compiler and an interpreter,
- 1:37:57those differences don't matter to us for
- 1:38:00the time being. The result is the same.
- 1:38:02Our PHP code is being compiled into
- 1:38:05machine code. After it's been compiled
- 1:38:07into machine code, our machines can
- 1:38:10execute the instructions written from
- 1:38:12our script. So, you may be wondering why
- 1:38:14is this process important to know?
- 1:38:17Unfortunately, the compilation process
- 1:38:19is not perfect. PHP struggles with
- 1:38:22decimal values. Whenever working with
- 1:38:24decimal numbers, PHP tends to round your
- 1:38:27values incorrectly.
- 1:38:31In the resource section of this lecture,
- 1:38:33I provide a link to a site called
- 1:38:35floating point guide. Issues with
- 1:38:38floating numbers are not specific to
- 1:38:40PHP. If you decide to switch to another
- 1:38:43language, you're likely to encounter
- 1:38:45this issue. This site is a helpful
- 1:38:47resource for developers to understand
- 1:38:50why floats are a problem. You don't have
- 1:38:52to read it. My explanation is more than
- 1:38:54enough to understand why this behavior
- 1:38:57occurs. However, if you're curious for
- 1:38:59more information, this resource can
- 1:39:01provide additional details. In the
- 1:39:03future, we're going to explore solutions
- 1:39:06for addressing these issues. For now,
- 1:39:08we'll leave the results alone. Back in
- 1:39:10our project, let's explore the other
- 1:39:13arithmetic operators. For the data
- 1:39:16variable, replace the value with the
- 1:39:18following equation. 11%
- 1:39:212.
- 1:39:24The percent character is known as the
- 1:39:26modulo operator. Sometimes you may not
- 1:39:29be able to divide numbers perfectly.
- 1:39:31It's always possible that you may have a
- 1:39:33remainder. The modulo operator performs
- 1:39:36division but it doesn't return the
- 1:39:38result. It returns the remainder. In
- 1:39:41this example, the remainder is one which
- 1:39:43is shown in the preview. It seems like a
- 1:39:46strange operator, but this operator is
- 1:39:48useful when trying to check if numbers
- 1:39:51are evenly divisible or detecting odd
- 1:39:53numbers. The last operator that I want
- 1:39:56to show you is the exponentiation
- 1:39:58operator. As the name suggests, it
- 1:40:01allows us to perform exponentiation with
- 1:40:03numbers. Let's replace the data
- 1:40:06variables value once more with the
- 1:40:08following value 10 asterisk asterisk 2.
- 1:40:14The exponentiation operator is performed
- 1:40:17with two asterisk characters. It's not
- 1:40:20to be confused with the multiplication
- 1:40:22operator which is just one asterisk. The
- 1:40:25number on the left side of the operator
- 1:40:27is considered to be the base whereas the
- 1:40:30number on the right is the exponent. If
- 1:40:32we look at the preview, we get 100.
- 1:40:35Perfect. There's one more thing I want
- 1:40:37to discuss before moving on. I'm going
- 1:40:39to revert the value of the data operator
- 1:40:42to the first equation we had at the
- 1:40:44beginning of the lecture. As we learned
- 1:40:47earlier, the order of operations is
- 1:40:49applied to our equation. We have the
- 1:40:51option of changing the order of
- 1:40:53operations by using parenthesis. For
- 1:40:56example, let's wrap the middle numbers
- 1:40:58with a pair of parenthesis.
- 1:41:02By doing so, PHP calculates the value
- 1:41:05from within the parenthesis before
- 1:41:07executing the other portions of the
- 1:41:09equation. By adding these parenthesis, a
- 1:41:12completely different result gets
- 1:41:14produced. You can fine-tune your
- 1:41:17equations with this simple syntax. As
- 1:41:19long as you know basic math operations,
- 1:41:22you shouldn't have a problem with them
- 1:41:23in PHP. In the next lecture, let's
- 1:41:26continue exploring other operators.
- 1:41:31[Music]
- 1:41:38In this lecture, we're going to continue
- 1:41:40our exploration of operators. In the
- 1:41:43resource section of this lecture, I
- 1:41:45provide a link to the documentation page
- 1:41:47for a complete list of assignment
- 1:41:49operators. Thus far, we've looked at a
- 1:41:51single assignment operator which is
- 1:41:53written with the equals character.
- 1:41:55However, this is not the only assignment
- 1:41:58operator offered by PHP. Other
- 1:42:01assignment operators are available that
- 1:42:03can be thought of as shorthand syntax
- 1:42:05for common operations. Let's look at a
- 1:42:08few examples in our project. Let's
- 1:42:10assign the data variable to 10.
- 1:42:15As we know, arithmetic operators allow
- 1:42:18us to perform mathematical operations
- 1:42:20from within our script. It's not
- 1:42:22uncommon to update a variable's value
- 1:42:25throughout the script. For example,
- 1:42:27let's say the data variable stores the
- 1:42:30total cost of a user's purchase. If
- 1:42:32users remove or add items, the data
- 1:42:35variable should be updated to reflect
- 1:42:38those changes. After setting the data
- 1:42:40variable, let's update the variable to
- 1:42:43the following value, data + 4.
- 1:42:47In this example, we're referencing the
- 1:42:50variable from within the value. PHP
- 1:42:52takes the current value and adds four,
- 1:42:55resulting in 14. There's actually a
- 1:42:57shorter way of writing this line of
- 1:42:59code. PHP introduces variations of the
- 1:43:02assignment operator for each of the
- 1:43:04arithmetic operators. There's a shorter
- 1:43:07way of writing this equation by adding
- 1:43:09the addition operator to the left side
- 1:43:11of the assignment operator. After adding
- 1:43:14this operator, we can remove the data
- 1:43:16variable from the equation.
- 1:43:20The results are the same. By adding the
- 1:43:22addition operator to the left of the
- 1:43:24assignment operator, PHP takes the
- 1:43:27existing value of the data variable and
- 1:43:29adds the value to the operator's right.
- 1:43:32This feature applies to all arithmetic
- 1:43:34operators. We can use subtraction,
- 1:43:39multiplication,
- 1:43:42division,
- 1:43:45modulo,
- 1:43:48and finally exponentiation.
- 1:43:53These assignment operators can help make
- 1:43:55your code look more readable. It's up to
- 1:43:57you if you want to use them. In the next
- 1:43:59lecture, let's keep exploring other
- 1:44:02operators offered by PHP.
- 1:44:04[Music]
- 1:44:15In this lecture, we're going to explore
- 1:44:17another set of operators called
- 1:44:19comparison operators. As the name
- 1:44:22implies, comparison operators are
- 1:44:24capable of comparing values. In the
- 1:44:26resource section of this lecture, I
- 1:44:29provide a link to a complete list of
- 1:44:31comparison operators. One thing to keep
- 1:44:34in mind is the results of the comparison
- 1:44:36operators. Arithmetic operators produced
- 1:44:39integers and floats whereas comparison
- 1:44:41operators produce boolean values. Under
- 1:44:44the results column, take notice of the
- 1:44:46data type. It's all booleans. So let's
- 1:44:50try using some of these operators inside
- 1:44:52our script. We're going to use the
- 1:44:54operators from within the variable dump
- 1:44:57function. We don't need the data
- 1:44:59variable anymore. PHP introduces two
- 1:45:02operators for comparing values. The
- 1:45:04first operator is written with two
- 1:45:06equals characters. This operator
- 1:45:09compares two values and returns a
- 1:45:11boolean. For example, let's try
- 1:45:13comparing one with one.
- 1:45:17It's important to note that we're using
- 1:45:19two equals characters. A common mistake
- 1:45:22among beginners is to use a single
- 1:45:24equals character, which as we know is
- 1:45:27the assignment operator. We don't want
- 1:45:29to assign a value but compare two
- 1:45:31values. In the preview, the operator has
- 1:45:34produced a true boolean value. Now,
- 1:45:37there's something worth noting about the
- 1:45:39equal operator, which is what this
- 1:45:41operator is called. Behind the scenes,
- 1:45:43PHP may perform type juggling. As a
- 1:45:46reminder, type juggling is a feature
- 1:45:49where PHP performs type casting
- 1:45:51automatically. If we attempt to compare
- 1:45:54two values with different data types,
- 1:45:56PHP performs type casting before
- 1:45:59comparing them. For example, let's dump
- 1:46:02another value by adding a comma. The
- 1:46:04variable dump function is not limited to
- 1:46:06rendering a single value. Multiple
- 1:46:09values can be rendered by separating
- 1:46:11them with commas. For the second value,
- 1:46:13let's compare one with a string that
- 1:46:16contains one.
- 1:46:19Technically, these are different values
- 1:46:21because of their data types. However,
- 1:46:23looking over the output, the value has
- 1:46:26been set to true. This result confirms
- 1:46:29the PHP's type juggling behavior.
- 1:46:32Otherwise, these values would not be
- 1:46:34comparable. While it may seem
- 1:46:36convenient, unexpected results may be
- 1:46:38produced. In most cases, developers
- 1:46:41prefer to compare values with the same
- 1:46:43data types. This produces the most
- 1:46:45reliable results. PHP allows us to
- 1:46:48compare values and data types by using
- 1:46:51three equals characters. Let's add an
- 1:46:54extra equals character to both values
- 1:46:56ducted into the output.
- 1:47:00This operator is more strict than the
- 1:47:02previous operator. The data types must
- 1:47:05match otherwise a false value is
- 1:47:08evaluated. In the preview, the results
- 1:47:10are different. This time the second
- 1:47:13boolean value is false. Despite having
- 1:47:16similar values, the data types don't
- 1:47:18match, meaning that PHP considers them
- 1:47:20to be different values. I always
- 1:47:23recommend strictly comparing values.
- 1:47:25Trust me, you'll save yourself a
- 1:47:27headache. Type juggling in these
- 1:47:29scenarios is better left off avoided. Up
- 1:47:32next, we have operators for comparing
- 1:47:34values that don't match. In our
- 1:47:36examples, I'm going to replace the first
- 1:47:39equals character with an exclamation
- 1:47:41point character.
- 1:47:45These operators are referred to as the
- 1:47:47not equal and not identical operators
- 1:47:50respectively. Unlike before, these
- 1:47:52operators attempt to check that the
- 1:47:54values don't match. If they don't match,
- 1:47:57the operator produces a true boolean
- 1:47:59value, otherwise we'll get false. The
- 1:48:02main difference between them is that the
- 1:48:04not equal operator may perform type
- 1:48:07juggling whereas the not identical
- 1:48:09operator does not perform type juggling.
- 1:48:12In the preview, the first comparison
- 1:48:14produces false because one and one
- 1:48:17completely match. However, the second
- 1:48:19comparison produces true since the
- 1:48:22values are different data types. One
- 1:48:24thing I want to mention is that there's
- 1:48:26an alternative syntax for the not equal
- 1:48:29operator. You can use the exclamation
- 1:48:31point equals characters or the less than
- 1:48:35greater than characters.
- 1:48:38There isn't a clear difference between
- 1:48:40these variations. However, the
- 1:48:42exclamation point equals characters are
- 1:48:45the most common combination. Let's keep
- 1:48:47going. Inside the variable dump
- 1:48:49function, we can compare values that are
- 1:48:52less than or greater than each other.
- 1:48:54Add the following values. 2 less than 3,
- 1:48:572 greater than 3, 2 less than equals 3,
- 1:49:012 greater than equals 3.
- 1:49:06The first two examples are
- 1:49:07self-explanatory. The less than
- 1:49:09character is used for checking if one
- 1:49:11value is less than another. Whereas the
- 1:49:13greater than character is used for
- 1:49:15checking if one value is greater than
- 1:49:17another. But what about the other two
- 1:49:19operators? They're similar to the first
- 1:49:22two examples. However, they will also
- 1:49:24check if two values are equal to one
- 1:49:27another. In the third example, we're
- 1:49:29checking if two is less than or equal to
- 1:49:32three. As long as the number on the left
- 1:49:34is less than or equal to three, PHP
- 1:49:37produces a true boolean value. The same
- 1:49:40idea applies to the fourth example, but
- 1:49:42with a greater than comparison. You can
- 1:49:45play around with these operators and
- 1:49:47check out the results to understand how
- 1:49:49they work. For now, that about does it
- 1:49:51for the comparison operators. Let's keep
- 1:49:54going in the next lecture.
- 1:49:56[Music]
- 1:50:06In this lecture, we're going to explore
- 1:50:08another operator or suppressing errors.
- 1:50:11Errors are a common occurrence in
- 1:50:13programming. You should always try your
- 1:50:15best to resolve an error. However, you
- 1:50:17may find yourself short on time. If
- 1:50:19that's the case, it's possible to
- 1:50:21suppress errors with the error control
- 1:50:24operator. Let's look at an example we're
- 1:50:27already familiar with. In the variable
- 1:50:29dump function, replace the value passed
- 1:50:32into the function with the following
- 1:50:33code. String left bracket one right
- 1:50:37bracket.
- 1:50:40In this example, we're type casting an
- 1:50:42array to a string. PHP doesn't like
- 1:50:45this. As you can see from the preview,
- 1:50:47we get a warning from our code. Despite
- 1:50:49the warning, PHP has typcasted the array
- 1:50:52into the word array. If we're short on
- 1:50:55time, we may not have time to properly
- 1:50:57address this error. PHP gives us the
- 1:51:00option of suppressing the error by
- 1:51:02adding the at character at the beginning
- 1:51:04of the line of code. If an error gets
- 1:51:06thrown, PHP ignores it. In this example,
- 1:51:10I'm converting an array into a string,
- 1:51:12which is not allowed. Typically, this
- 1:51:14line of code throws an error. Thanks to
- 1:51:16the error control operator, nothing will
- 1:51:19be output onto the screen. I really want
- 1:51:22to emphasize that you should use this
- 1:51:24operator sparingly. PHP has plenty of
- 1:51:27features to help you avoid errors in
- 1:51:29your code. This operator should almost
- 1:51:31never be necessary. Despite that, it may
- 1:51:34come in handy one day. In the next
- 1:51:36lecture, let's keep exploring other
- 1:51:38operators.
- 1:51:40[Music]
- 1:51:50In this lecture, we're going to learn
- 1:51:52about the increment and decrement
- 1:51:54operators. These operators were
- 1:51:56introduced for updating integers by one.
- 1:51:59Let's look at an example to understand
- 1:52:01how they work. In our script, let's set
- 1:52:04the data variable to a random number
- 1:52:06like five.
- 1:52:09In some cases, we may want to update
- 1:52:12this number by one. For example, you may
- 1:52:14want to keep track of the items in a
- 1:52:16user's cart. If they add an item, you
- 1:52:19may want to update the cart's size by
- 1:52:21one. A possible solution would be to do
- 1:52:24the following. Data equals data + one.
- 1:52:29That works, but we can shorten this code
- 1:52:32even further by using the addition
- 1:52:34assignment operator like so.
- 1:52:38However, there's an even shorter
- 1:52:40solution, which is the increment
- 1:52:42operator. If your goal is to update a
- 1:52:44number by one, you can do the following
- 1:52:47data ++.
- 1:52:50The plus+ characters are known as the
- 1:52:53increment operator. It'll update a value
- 1:52:55by one. Let's update the variable dump
- 1:52:58function to output the data variable.
- 1:53:02In the preview, the new value is six.
- 1:53:05Numbers can be decremented too, which is
- 1:53:08subtracting the current value by one.
- 1:53:10Instead of using the plus plus
- 1:53:12characters, they can be replaced with
- 1:53:14the minus minus characters.
- 1:53:18This time the page displays four. These
- 1:53:21operators are a useful shortcut. There's
- 1:53:23one thing to keep in mind when using
- 1:53:25these operators. The location of the
- 1:53:27operator does affect the behavior of the
- 1:53:30operator. If we add the operators after
- 1:53:33the value, PHP returns the value. After
- 1:53:36doing so, it'll increment the value.
- 1:53:39These steps are not performed together.
- 1:53:41You might want the value to be updated
- 1:53:43before it's returned. For example, we
- 1:53:46may want to increment the value while
- 1:53:48dumping it.
- 1:53:51In the preview, the value remains five
- 1:53:54despite incrementing it in our code.
- 1:53:57This is because PHP is passing on the
- 1:53:59value to the function before
- 1:54:01incrementing it. Therefore, the original
- 1:54:04value gets rendered on the screen. If we
- 1:54:06want to wait for the value to be
- 1:54:08incremented before outputting it, we can
- 1:54:10position the increment operator before
- 1:54:13the variable like so.
- 1:54:17Now the value has been incremented
- 1:54:19before being displayed. This same
- 1:54:21behavior applies to the decrement
- 1:54:23operator. Immediately using a variable
- 1:54:26after it's been updated is common. If
- 1:54:28the variable is not updated properly,
- 1:54:31you may want to consider moving it to
- 1:54:33the other side of the variable. So
- 1:54:35that's the increment and decrement
- 1:54:38operators. Let's keep exploring other
- 1:54:40operators in the next lecture.
- 1:54:43[Music]
- 1:54:53In this lecture, we're going to start
- 1:54:55exploring logical operators. Comparing
- 1:54:58values is a common action within PHP.
- 1:55:01However, you may want to perform
- 1:55:03multiple comparisons. At the moment,
- 1:55:06we're limited to a single comparison.
- 1:55:08Logical operators were introduced to
- 1:55:10allow developers to perform multiple
- 1:55:13comparisons. In our script, let's say we
- 1:55:16wanted to limit our user base to users
- 1:55:18between the ages of 18 and 65. Above the
- 1:55:22data variable, define a variable called
- 1:55:25age with a value of 29.
- 1:55:30Notice how I said the word define.
- 1:55:32Sometimes developers refer to the
- 1:55:35creation of a variable as a variable
- 1:55:37declaration. You may hear developers say
- 1:55:40define a variable or declare a variable.
- 1:55:43They all mean the same thing. Just
- 1:55:45thought you should be aware of that. All
- 1:55:47right, moving along. Let's set the data
- 1:55:50variable to the following. Age greater
- 1:55:52than 18 and age less than 65.
- 1:55:58In addition, let's replace the operator
- 1:56:00from within the variable dump function.
- 1:56:05So the and operator allows us to add an
- 1:56:09additional comparison instead of one.
- 1:56:11This operator requires that both
- 1:56:13comparisons evaluate to true. If both
- 1:56:16comparisons are truthy, the value of the
- 1:56:18data variable becomes a boolean true.
- 1:56:21Otherwise, the value becomes false. For
- 1:56:23example, let's set the age variable to
- 1:56:2615.
- 1:56:29Technically, the age is under 65, but
- 1:56:32the age is not ever 18. Therefore, the
- 1:56:35data variable evaluates to false.
- 1:56:38There's another thing worth mentioning
- 1:56:40about the and operator. The and operator
- 1:56:43can be written with two amperand
- 1:56:46characters or the word and.
- 1:56:50Both solutions are viable. However, most
- 1:56:53developers prefer the amperand
- 1:56:55variation. While the and operator is
- 1:56:58great, you may want either condition to
- 1:57:00be true. For example, let's create a
- 1:57:03variable called permission with a value
- 1:57:06of admin.
- 1:57:09Restricting access to specific pages is
- 1:57:12a common practice. You may not want
- 1:57:14users to view the settings of a site
- 1:57:16unless they have administrative or
- 1:57:18moderation powers. It's unlikely that
- 1:57:21users are going to have both
- 1:57:22permissions. So, we might just want to
- 1:57:25check for one of these permissions. For
- 1:57:27the data variable, let's use the
- 1:57:29following value. permission equals
- 1:57:32equals equals admin or permission equals
- 1:57:35equals equals mod.
- 1:57:40The or operator can be written with the
- 1:57:42word or or with two pipe characters.
- 1:57:48The pipe variation is more common and
- 1:57:50preferred. If one condition evaluates to
- 1:57:53true and the other condition evaluates
- 1:57:55to false, the entire value evaluates to
- 1:57:58true. Overall, the or operator allows
- 1:58:01for either comparison to be truthy for
- 1:58:03the entire line of code to evaluate to a
- 1:58:06truthy value. There's one more logical
- 1:58:09operator variable called the not
- 1:58:11operator. Thus far, everything checks
- 1:58:14for a truthy value. What if you want to
- 1:58:16test for a false value instead of a true
- 1:58:19value? You can do so with the not
- 1:58:21operator, which is written with the
- 1:58:23exclamation point character. You can add
- 1:58:26this character to the beginning of the
- 1:58:28value. For example, let's use the
- 1:58:30following value for the data variable,
- 1:58:33not permission.
- 1:58:36Next, let's leave the permission
- 1:58:38variable as an empty string.
- 1:58:42By using the not operator, PHP
- 1:58:45transforms a value into a boolean before
- 1:58:48checking the value. After doing so, PHP
- 1:58:51checks for a false value instead of a
- 1:58:54true value. If the value is false, the
- 1:58:56operator evaluates a true boolean value.
- 1:59:00As you can see in the preview, the value
- 1:59:02of the data variable is true.
- 1:59:04Technically, empty strings are
- 1:59:06considered to be false. But by using the
- 1:59:09not operator, that'll allow us to check
- 1:59:11for a false value. That's it for logical
- 1:59:14operators. In the next lecture, we're
- 1:59:17going to continue our discussion of
- 1:59:19operators.
- 1:59:20[Music]
- 1:59:31In this lecture, we're going to start
- 1:59:33focusing on operator precedents. PHP
- 1:59:36offers dozens of operators. Best of all,
- 1:59:39we can use various operators together.
- 1:59:42However, that can potentially be an
- 1:59:44issue if we're not careful. We may
- 1:59:46receive unexpected results. How does PHP
- 1:59:49know which operator to use over the
- 1:59:51other? In the resource section of this
- 1:59:54lecture, I provide a link to a
- 1:59:56documentation page called operator
- 1:59:58precedence. On this page, we're
- 2:00:01presented with a table of operators.
- 2:00:03Operators listed at the top of the table
- 2:00:06have higher precedence than operators
- 2:00:08listed lower on the table. If we have a
- 2:00:11value with multiple operators, PHP
- 2:00:14executes the operators according to
- 2:00:16their precedence. For example, the
- 2:00:19multiplication and division operators
- 2:00:22have higher priority than the addition
- 2:00:24and subtraction operators. In our
- 2:00:27script, let's clear the contents of the
- 2:00:29file. In its place, define a variable
- 2:00:32called a with the following value 5 + 2
- 2:00:36asterisk 10.
- 2:00:40In this example, we have three operators
- 2:00:43which are the assignment, addition and
- 2:00:46multiplication operators. As we know,
- 2:00:49the multiplication operator has the
- 2:00:51higher precedence. Therefore, this part
- 2:00:54of the code is evaluated first. This is
- 2:00:56followed by the addition operator. If
- 2:00:59you were to look through the table, the
- 2:01:01assignment operator is listed at the
- 2:01:03bottom of the table. It's safe to
- 2:01:05conclude that the assignment operator
- 2:01:07gets executed last. Hopefully things are
- 2:01:10making sense on how PHP deals with
- 2:01:13multiple operators. Let's take things a
- 2:01:15step further. What if we have operators
- 2:01:18with the same precedence? For example,
- 2:01:21let's replace the addition operator with
- 2:01:24the division operator.
- 2:01:27How does PHP handle these scenarios? It
- 2:01:30turns out PHP has an answer for us. Back
- 2:01:33at the table, there's a column called
- 2:01:36associivity. If operators have the same
- 2:01:38precedence, PHP executes them in the
- 2:01:41order specified under this column. For
- 2:01:44instance, PHP executes these operators
- 2:01:47from left to right. Therefore, we can
- 2:01:50safely assume PHP starts with division.
- 2:01:53After division has been performed,
- 2:01:56multiplication is performed. In some
- 2:01:58cases, you may want the opposite to
- 2:02:00occur. What if we want multiplication to
- 2:02:03be performed first? In that case, we can
- 2:02:06wrap this portion of the value with
- 2:02:08parenthesis.
- 2:02:11Parentheses give us the option to refine
- 2:02:14the order of operations for all
- 2:02:16operators. You should consider using
- 2:02:18parentheses when you aren't getting the
- 2:02:21desired results. On the other hand, not
- 2:02:24all operators support associivity. Let's
- 2:02:27head back to the table. According to the
- 2:02:29table, comparison operators do not have
- 2:02:32associivity. If a group of operators
- 2:02:34don't have associivity, this means they
- 2:02:37can't be used from within a single
- 2:02:39value. For example, let's head back to
- 2:02:42our code. Update the variable's value to
- 2:02:45the following. 5 less than 2, greater
- 2:02:48than 8.
- 2:02:52We can't use the greater than and less
- 2:02:55than operators within a single
- 2:02:57expression. PHP would throw an error at
- 2:02:59us. Keep this in mind when working with
- 2:03:02these operators. There's one last thing
- 2:03:04I want to mention about operator
- 2:03:06precedence. From time to time, PHP
- 2:03:10offers two operators for performing the
- 2:03:12same thing. For example, let's set the
- 2:03:15variable to the following, true and
- 2:03:17false.
- 2:03:20In this scenario, I'm using the amperand
- 2:03:23characters for the AND operator. As we
- 2:03:26know the alternative syntax for the and
- 2:03:28operator is the word and. Both operators
- 2:03:32allow us to write multiple conditions.
- 2:03:34However, there is a difference. The
- 2:03:36difference stems from the operator's
- 2:03:38precedence. After setting the variable,
- 2:03:41let's log the variable with the variable
- 2:03:44dump function.
- 2:03:47As expected, a false value gets
- 2:03:50evaluated. Both conditions must be true
- 2:03:53for a true value to be produced. Since
- 2:03:56the second value is false, we'll receive
- 2:03:58false. What do you think would happen if
- 2:04:01we switched operators? Let's find out.
- 2:04:03Change the operator to and.
- 2:04:08This time the variable has been set to
- 2:04:10true. Isn't that strange? Both operators
- 2:04:13perform the same logical operation. So
- 2:04:16why does the variable have a different
- 2:04:18value? The answer can be found in the
- 2:04:21table. The and operator has a lower
- 2:04:24precedence than the assignment operator.
- 2:04:26Since that's the case, PHP sets the
- 2:04:29variable to true before checking the
- 2:04:32second value. After the variable has
- 2:04:34been set, PHP throws away the second
- 2:04:37portion of the expression. This behavior
- 2:04:39can be a gotcha. To avoid this scenario
- 2:04:42from occurring, you can always use the
- 2:04:44original variation of the operator. This
- 2:04:47variation has a higher precedence than
- 2:04:49the assignment operator. In most cases,
- 2:04:52you should avoid the and, and or
- 2:04:54keywords. You should use their other
- 2:04:57variations since these operators have
- 2:04:59higher precedence, therefore guaranteed
- 2:05:02to run before a value is assigned. For
- 2:05:05the rest of this course, I'll use
- 2:05:07variations with higher precedence.
- 2:05:10[Music]
- 2:05:20In this lecture, we're going to explore
- 2:05:23another type of variable called a
- 2:05:24constants. Variables are flexible. We
- 2:05:27have the option of overwriting the value
- 2:05:30of a variable after it's been declared.
- 2:05:32Let me give an example. First, I'm going
- 2:05:35to comment the code we've written thus
- 2:05:36far. It's starting to get cluttered.
- 2:05:41Now that our PHP file has been
- 2:05:43commented, let's define a new variable
- 2:05:45called full name. The value for this
- 2:05:48variable will be John Smith.
- 2:05:52After defining a variable, it can be
- 2:05:54updated to a different value. For
- 2:05:57example, let's update the variable to
- 2:05:59100.
- 2:06:02After updating the variable, call the
- 2:06:05variable dump function with the full
- 2:06:07name variable.
- 2:06:10the value of the variable has been
- 2:06:12updated correctly. PHP makes it
- 2:06:14extremely easy to update variables. On
- 2:06:17the other hand, this behavior can be
- 2:06:19problematic. Variables can be accessed
- 2:06:22through multiple PHP files. At the
- 2:06:25moment, we're working on a singular PHP
- 2:06:27file. For larger projects, it's common
- 2:06:30practice to split your codebase into
- 2:06:32multiple files. That can lead to issues.
- 2:06:35Variables can be used across multiple
- 2:06:37files. What if you accidentally update a
- 2:06:40variable? An accidental update can lead
- 2:06:42to errors in other files. In some cases,
- 2:06:45you may want your variables to contain
- 2:06:48the same value throughout the lifetime
- 2:06:50of a program. PHP has a feature for
- 2:06:53creating a variable that can't have its
- 2:06:55value changed called a constant. If we
- 2:06:58were to attempt to update a constant
- 2:07:00after it's been created, an error would
- 2:07:02get thrown. Constants are treated as
- 2:07:04read only. Let's try creating a
- 2:07:06constants. Below the full name variable,
- 2:07:09add a keyword called const.
- 2:07:14Using the const keyword instructs PHP to
- 2:07:17create a constant. After the const
- 2:07:19keyword, we must provide a name for a
- 2:07:22variable. Unlike before, we do not need
- 2:07:24to start the variable name with the
- 2:07:26dollar sign character. The other rules
- 2:07:28for creating a variable still apply. We
- 2:07:31can use alpha numeric and underscore
- 2:07:33characters. Special characters are not
- 2:07:36allowed. Lastly, we can't use reserved
- 2:07:38keywords. For example, we can't set the
- 2:07:41name to const.
- 2:07:45By typing this keyword, we're given an
- 2:07:47error. PHP assumes that we're trying to
- 2:07:50create a constant while attempting to
- 2:07:52create a constant. This code leaves the
- 2:07:54language confused. Let's set the name to
- 2:07:57full name.
- 2:08:00The name of the variable uses all
- 2:08:02uppercase letters. This step is
- 2:08:04completely optional. We could have used
- 2:08:06camel casing, Pascal casing, or snake
- 2:08:09casing. However, constants behave
- 2:08:11differently than regular variables. For
- 2:08:14this reason, developers like to use all
- 2:08:16uppercase letters for constants. It
- 2:08:19helps other developers identify the
- 2:08:21variable as a constant. More often than
- 2:08:24not, this naming convention is widely
- 2:08:26adopted by the community. This includes
- 2:08:28the PHP language. In the resource
- 2:08:31section of this lecture, I provide a
- 2:08:33link to PHP's official list of
- 2:08:35predefined constants. You should avoid
- 2:08:38using the same names listed on this
- 2:08:40page. Otherwise, you may get an error
- 2:08:43stating that you're attempting to update
- 2:08:44an existing constant. Luckily, most of
- 2:08:47PHP's constants start with the word PHP.
- 2:08:51As long as you avoid starting your
- 2:08:52constant name with this word, you're
- 2:08:54good to go. Let's head back to our
- 2:08:56editors. We're going to be using all
- 2:08:59uppercase letters for our constant
- 2:09:01names. Once again, not required, but
- 2:09:04recommended. Let's set the value of this
- 2:09:06constant to John Smith.
- 2:09:10Next, we can output the variable by
- 2:09:13updating the variable dump function to
- 2:09:15use the constant.
- 2:09:19To read a value from a constant, we can
- 2:09:21type its name as is. We do not need to
- 2:09:24include the dollar sign character. The
- 2:09:26value of the constant has been outputed.
- 2:09:29As stated before, we cannot change the
- 2:09:31value after it's been declared. For
- 2:09:33example, I'm going to try updating the
- 2:09:36full name constant to a different name.
- 2:09:41An error gets outputed from the program.
- 2:09:43According to PHP, the full name constant
- 2:09:46has already been created. We're not
- 2:09:48allowed to update its value. This is the
- 2:09:50benefit of using a constant. We're
- 2:09:53restricted to the same value for the
- 2:09:55rest of the program. If we attempt to
- 2:09:57update it, our program will stop
- 2:09:59working. PHP will always notify us when
- 2:10:02a constant is already defined. We'll be
- 2:10:04using constants to help us secure our
- 2:10:07script. Once we start working on a real
- 2:10:09project, you'll find out why they can be
- 2:10:11incredibly useful. In the next lecture,
- 2:10:14let's keep exploring more PHP features.
- 2:10:17[Music]
- 2:10:28In this lecture, we're going to look at
- 2:10:30an alternative solution to injecting
- 2:10:32variables into strings called string
- 2:10:34concatenation. As we learned from a
- 2:10:36previous lecture, string interpolation
- 2:10:38can be a great solution to inserting a
- 2:10:41variable into a string. However, string
- 2:10:43interpolation is only available for
- 2:10:45regular variables. Constants are not
- 2:10:48supported in string interpolation. What
- 2:10:50if we need to insert a constant into a
- 2:10:53string? There's an alternative solution
- 2:10:55called string concatenation. In our
- 2:10:58script, we have two constants called
- 2:11:00full name. Let's remove the second copy.
- 2:11:05Next, let's add the following string
- 2:11:07inside the variable dump function.
- 2:11:10Hello, my name is.
- 2:11:14At the end of the string, we may want to
- 2:11:16insert the full name constants. A
- 2:11:18constant can be added using string
- 2:11:21concatenation. String concatenation is
- 2:11:24the process of combining two strings
- 2:11:26into a single string. We can perform
- 2:11:28string concatenation with the
- 2:11:30concatenation operator. This operator
- 2:11:33can be written with the dot character.
- 2:11:35After the string, add this character.
- 2:11:40PHP will combine both strings into a
- 2:11:43single string. The output contains both
- 2:11:46strings. However, there's one problem.
- 2:11:48Space does not separate both strings,
- 2:11:50which makes the sentence look awkward.
- 2:11:53This behavior makes sense when you think
- 2:11:55about it. In the first string, we don't
- 2:11:57have a space at the end of the string.
- 2:11:59The constant does not contain a space at
- 2:12:01the beginning of the string. If we were
- 2:12:03to add a space at the end of the first
- 2:12:05string, this update should resolve our
- 2:12:08errors.
- 2:12:10As you can see, the space has been
- 2:12:12added. It's a common mistake to forget
- 2:12:14to add spaces when concatenating things.
- 2:12:17String concatenation and string
- 2:12:19interpolation are powerful features for
- 2:12:22inserting variables into a string
- 2:12:24whenever possible. I prefer to use
- 2:12:26string interpolation. In my opinion,
- 2:12:29it's easier to read. If I'm dealing with
- 2:12:31constants, I'll use concatenation since
- 2:12:34interpolation does not support
- 2:12:36constants. You may prefer concatenation
- 2:12:39because it can support strings with
- 2:12:41single or double quotes. Other than
- 2:12:43that, it all comes down to preference.
- 2:12:46Just like arithmetic operators, the
- 2:12:48concatenation operator can be used with
- 2:12:50the assignment operator. For example,
- 2:12:53let's outsource the first portion of the
- 2:12:55string to a variable called message.
- 2:13:00Next, let's type the following after
- 2:13:02defining the variable. Message dot
- 2:13:05equals full name.
- 2:13:08Similar to arithmetic operators, we can
- 2:13:11add the concatenation operator to the
- 2:13:13left of the assignment operator. PHP
- 2:13:16will add the string to the existing
- 2:13:18string. This results in the same thing
- 2:13:20as before. Let's update the variable
- 2:13:23dump function to output the message
- 2:13:25variable.
- 2:13:28Perfect. Our message works like before.
- 2:13:30The concatenation assignment operator
- 2:13:33can be useful when you're trying to add
- 2:13:35to an existing string. That is about it
- 2:13:37for this section. We spent a lot of time
- 2:13:40talking about variables and data types.
- 2:13:42Data is an essential component of any
- 2:13:45application you're building, but how can
- 2:13:47we use this data to create dynamic
- 2:13:49pages? That's going to be the focus of
- 2:13:52the next section. When you're ready,
- 2:13:54I'll see you there.
- 2:13:55[Music]
- 2:14:05In this lecture, we're going to quickly
- 2:14:08discuss an important word called
- 2:14:10expressions. Programming can be
- 2:14:12challenging to grasp because of the
- 2:14:14terminology. Learning the terminology of
- 2:14:16a language is half the battle. If you're
- 2:14:19able to understand the lingo used by
- 2:14:21developers, you're well on your way to
- 2:14:23learning a new language. The word
- 2:14:25expression is going to come up a lot
- 2:14:27throughout the course. So, what are
- 2:14:29expressions? An expression is a line of
- 2:14:32code that evaluates to a value. It's as
- 2:14:34simple as that. Believe it or not, we've
- 2:14:37been writing expressions for most of
- 2:14:39this course. For example, when we create
- 2:14:42a variable, PHP doesn't immediately
- 2:14:44assign the value to the variable. Before
- 2:14:47it does, it'll attempt to evaluate the
- 2:14:49value on the right of the assignment
- 2:14:51operator. In this example, this process
- 2:14:54happens immediately because the value is
- 2:14:57ready as is. The number five evaluates
- 2:14:59to five. After the expression has been
- 2:15:02evaluated, the value is assigned to the
- 2:15:05variable. The same behavior occurs for
- 2:15:07mathematical operations. In this
- 2:15:10example, PHP doesn't immediately assign
- 2:15:12a value to the variable. It'll notice
- 2:15:15that we're performing addition. This
- 2:15:17expression gets evaluated before
- 2:15:19anything else happens. After a value has
- 2:15:22been produced, the value gets assigned
- 2:15:24to the variable. Now you may be thinking
- 2:15:27what would be the difference between
- 2:15:29operators and expressions. They have
- 2:15:31similar definitions. Both terms are used
- 2:15:34to describe operations that produce a
- 2:15:37value. Well, an operator refers to
- 2:15:40characters or symbols that perform an
- 2:15:42operation. As we know, we can have
- 2:15:45multiple operators from a single line of
- 2:15:47code. In comparison, expressions refer
- 2:15:50to the entire line of code that produces
- 2:15:53a value. This includes the operators.
- 2:15:56This behavior is important to
- 2:15:58understand. A lot of code is going to
- 2:16:00contain expressions. Anytime you write
- 2:16:02code that produces a value, you've
- 2:16:05written an expression. In the resource
- 2:16:08section of this lecture, I provide a
- 2:16:10link to the echo keyword. Previously, we
- 2:16:13talked about the data type of the value
- 2:16:15expected by this keyword. Now I want to
- 2:16:18shift our focus to the name of the
- 2:16:20variable. Do you notice anything? The
- 2:16:22variable is referred to as an
- 2:16:24expression. PHP's documentation heavily
- 2:16:27uses the word expression, which makes
- 2:16:30sense, right? After all, PHP expects
- 2:16:33some kind of value to be written after
- 2:16:35the echo keyword. We have the freedom to
- 2:16:38calculate a value after the echo
- 2:16:40keyword. Therefore, the code written
- 2:16:42after the echo keyword can be considered
- 2:16:44an expression. Whenever you see the word
- 2:16:47expression in the documentation, just
- 2:16:49know that PHP is saying that you can
- 2:16:51write code that produces a value. In the
- 2:16:54next lecture, let's start using this
- 2:16:56knowledge to help us learn new features
- 2:16:58of PHP.
- 2:17:00[Music]
- 2:17:10In this lecture, we're going to learn
- 2:17:12how to control the flow of a program.
- 2:17:15Thus far, every line of code we've
- 2:17:17written has been executed. We don't have
- 2:17:19control over the flow of logic in our
- 2:17:21program. What if we want to execute code
- 2:17:23when a condition is true? For example,
- 2:17:26specific pages should be locked unless a
- 2:17:28user is logged in. Another example would
- 2:17:31be displaying administration features
- 2:17:33for users with specific privileges.
- 2:17:36There are hundreds of use cases for
- 2:17:38performing logic based on a condition.
- 2:17:40Luckily, PHP provides a solution for
- 2:17:43controlling the flow of logic called
- 2:17:45conditional statements. In the resource
- 2:17:48section of this lecture, I provide a
- 2:17:50link to the if keyword. This feature
- 2:17:52allows us to perform logic based on a
- 2:17:55condition. Let's read the documentation
- 2:17:57together. In the second paragraph, the
- 2:17:59following is stated as described in the
- 2:18:02section about expressions. Expression is
- 2:18:04evaluated to its boolean value. If
- 2:18:07expression evaluates to true, PHP will
- 2:18:10execute statement and if it evaluates to
- 2:18:13false, it'll ignore it. As we learned,
- 2:18:16expressions are lines of code that
- 2:18:18evaluate to a value. The if keyword
- 2:18:21expects a boolean value. This is
- 2:18:23important to understand. PHP does not
- 2:18:26use integers, floats, or strings.
- 2:18:28Booleans are used to determine whether a
- 2:18:31block of code is executed. In a
- 2:18:33nutshell, PHP performs type juggling on
- 2:18:36expressions that aren't booleans to
- 2:18:38convert the value into a boolean. Let's
- 2:18:41dive into the code to understand this
- 2:18:43feature. Switch over to our project. In
- 2:18:46the PHP file, I'm going to remove the
- 2:18:48existing code.
- 2:18:52As always, be sure to leave the opening
- 2:18:54PHP tag intact. Next, imagine a scenario
- 2:18:58of an application that displays a
- 2:19:00student's grade based on their score. In
- 2:19:03the file, define a variable called score
- 2:19:06with an initial value of 95.
- 2:19:11In America, the grading system can
- 2:19:13contain values between 0 and 100. 100 is
- 2:19:17considered passing. Zero is considered
- 2:19:19failing. Our goal is to inform the
- 2:19:22student that they've passed or failed
- 2:19:24based on their score. After the
- 2:19:26variable, add the if keyword.
- 2:19:30Adding this keyword instructs our
- 2:19:32program to check a condition. The
- 2:19:35condition can be written by adding a
- 2:19:37pair of parenthesis. Within these
- 2:19:39parentheses, we must provide an
- 2:19:41expression that evaluates to a boolean
- 2:19:43value. Let's pass in the score variable.
- 2:19:49The score variable has the integer data
- 2:19:52type. Regardless, this is considered a
- 2:19:55valid expression for the if statement.
- 2:19:57The reason is simple. Behind the scenes,
- 2:20:00PHP performs type juggling for values
- 2:20:03passed into the condition. As we know,
- 2:20:05integers are converted into true if the
- 2:20:08number is not zero. Since 95 is not
- 2:20:10zero, the expression evaluates to true.
- 2:20:13If the expression evaluates to true, PHP
- 2:20:16executes a block of code. The block of
- 2:20:19code can be written inside a pair of
- 2:20:21curly brackets. Let's add these
- 2:20:23characters after the parenthesis.
- 2:20:27Inside the brackets, call the variable
- 2:20:30dump function with the following value
- 2:20:33a.
- 2:20:36As a side note, the code inside the
- 2:20:38conditional statement is indented. This
- 2:20:40formatting is not required but
- 2:20:42recommended. It adds clarity that this
- 2:20:45section of code belongs to the
- 2:20:47conditional statement. It's a common
- 2:20:49practice adopted by developers. will be
- 2:20:52following it too. To recap, if the
- 2:20:54expression evaluates to true, the letter
- 2:20:57A should appear. In the preview, the
- 2:20:59letter has been dumped. While that's
- 2:21:01great, it's not exactly what we were
- 2:21:03aiming for. The same grade gets outputed
- 2:21:06regardless of the number. For example,
- 2:21:09let's set the score to one.
- 2:21:13An appropriate letter grade for the
- 2:21:15score would be an F. As it stands, the
- 2:21:18same letter grade appears on the page.
- 2:21:20by itself. Using the score as the
- 2:21:22condition produces undesirable results.
- 2:21:26Luckily, we know how to resolve this
- 2:21:28issue. We can use comparison operators.
- 2:21:31They're perfect for our scenario.
- 2:21:33Instead of using the score, we're going
- 2:21:34to compare the score with a specific
- 2:21:37number. If the score lands above a
- 2:21:39threshold, the appropriate letter grade
- 2:21:41should appear. PHP offers various
- 2:21:44comparison operators. Best of all, all
- 2:21:47comparison operators produce a boolean
- 2:21:49value, which is what the if keyword
- 2:21:52expects. In the conditional statement,
- 2:21:54update the expression to the following
- 2:21:56score greater than 90.
- 2:22:00Let's look at the preview. This time,
- 2:22:03nothing appears. It's because the
- 2:22:05condition was evaluated to false. If the
- 2:22:07if statement receives a false value, the
- 2:22:10block of code nested inside is not
- 2:22:12executed. But what if we want to output
- 2:22:15an alternative message? After the
- 2:22:17closing curly bracket, we can add an
- 2:22:19else block.
- 2:22:22The else keyword is optional. If it's
- 2:22:25added, PHP will execute code inside the
- 2:22:28else block when the condition from the
- 2:22:30if block fails. There is one rule we
- 2:22:32must follow. The else block must be
- 2:22:35added at the end of the if condition. If
- 2:22:38we were to attempt to write code between
- 2:22:40these keywords, PHP would throw an
- 2:22:42error. Inside the else block, let's call
- 2:22:45the variable dump function with the
- 2:22:48following value f.
- 2:22:52We can take this solution further by
- 2:22:54adding an else- if statement. In between
- 2:22:56these blocks of code, add the elseif
- 2:22:59keyword.
- 2:23:02So far, we have one condition. In some
- 2:23:04cases, we may want to write multiple
- 2:23:07conditions. PHP allows developers to
- 2:23:09chain multiple conditions with the LC if
- 2:23:12keyword. Unlike the if and else
- 2:23:15keywords, we can repeat the else if
- 2:23:17keyword as many times as we'd like.
- 2:23:19However, we may only write this keyword
- 2:23:22after an if statement and before an else
- 2:23:25statement. Inside the parenthesis, we
- 2:23:28can write a condition. Let's write the
- 2:23:30following condition. Score greater than
- 2:23:3280.
- 2:23:35Inside the block of code, call the
- 2:23:37variable dump function with the
- 2:23:39following value B.
- 2:23:43Just for fun, let's add another else if
- 2:23:46statement. This time the condition will
- 2:23:48be score greater than 70.
- 2:23:52Lastly, let's output the letter C with
- 2:23:55the variable dump function.
- 2:23:59Believe it or not, there are variations
- 2:24:01of the else if keyword. It's completely
- 2:24:04optional to add a space between the
- 2:24:06words else and if. I'll remove the space
- 2:24:09as a demonstration.
- 2:24:12For most projects, you should stick with
- 2:24:15consistent formatting instead of mixing
- 2:24:17and matching keywords. Let's check out
- 2:24:19our preview. As you can see, the letter
- 2:24:21F has appeared. That's to be expected
- 2:24:24since the score is too low for each
- 2:24:26condition. PHP defaults to the code from
- 2:24:28the else block which doesn't require a
- 2:24:31condition. Let's try updating the score
- 2:24:33to 95.
- 2:24:36Unlike before, the letter A has been
- 2:24:38outputed. But what about the letters B
- 2:24:41and C? PHP always starts from the top.
- 2:24:44If the first condition fails, it'll move
- 2:24:46on to the next condition. It'll continue
- 2:24:49this process in the order the conditions
- 2:24:51are written. Once it finds a condition
- 2:24:53that evaluates to true, it will not
- 2:24:55bother checking other conditions.
- 2:24:58Technically, the other conditions
- 2:25:00evaluate to true. However, since the
- 2:25:02first condition evaluates to true, the
- 2:25:05other conditional blocks of code are
- 2:25:07never executed. Hence why we're getting
- 2:25:09a single letter. Conditional statements
- 2:25:12are extremely powerful. They allow us to
- 2:25:14control the logic flow by executing code
- 2:25:17based on a condition. There's one more
- 2:25:19thing I want to mention before moving
- 2:25:21on. Unlike before, we're not adding the
- 2:25:24semicolon character at the end of each
- 2:25:26block of code. If we're ever using curly
- 2:25:29brackets for blocks of code, the
- 2:25:31semicolon character is not required.
- 2:25:34Using the closing curly bracket will
- 2:25:36suffice. Just thought you should know in
- 2:25:38case you notice that we're omitting the
- 2:25:40semicolon. In the next lecture, let's
- 2:25:43continue exploring other features for
- 2:25:45controlling logic flow.
- 2:25:47[Music]
- 2:25:57In this lecture, we're going to look at
- 2:25:59an alternative solution for controlling
- 2:26:02logic flow. Switch statements are
- 2:26:04another feature of PHP for performing a
- 2:26:07condition. There are a few differences,
- 2:26:09so let's get into them. In the PHP file,
- 2:26:12let's clear the contents.
- 2:26:16Next, add the switch keyword with a pair
- 2:26:18of parenthesis.
- 2:26:21The biggest difference between if
- 2:26:23statements and switch statements are the
- 2:26:25condition. Conditions for if statements
- 2:26:28are flexible. We can compare values any
- 2:26:30way we'd like. For example, we can check
- 2:26:33if one value is greater than another.
- 2:26:36Alternatively, we can check for things
- 2:26:38like the length of a string or if the
- 2:26:40user is logged in. On the other hand,
- 2:26:43switch statements compare values for
- 2:26:45equality. If two values match, a portion
- 2:26:48of the code will be executed. Let's look
- 2:26:51at an example. Above the switch keyword,
- 2:26:54create a variable called payment status
- 2:26:57with a value of one.
- 2:27:01It's common for developers to use
- 2:27:03numbers to represent the status of an
- 2:27:05action. In this example, we may want to
- 2:27:08store a user's payment status. The
- 2:27:10problem with this value is that numbers
- 2:27:12aren't descriptive. We wouldn't output a
- 2:27:15number to the user. Instead, we may want
- 2:27:18to output a friendlier message. This is
- 2:27:21the perfect case for a switch statement.
- 2:27:23Inside the parenthesis, add the payment
- 2:27:26status variable.
- 2:27:30The switch statement accepts a value.
- 2:27:32The value does not need to be a boolean.
- 2:27:35It can be an integer, float, or string.
- 2:27:38Next, inside a pair of curly brackets,
- 2:27:40we must add a keyword called case.
- 2:27:45After the case keyword, we must add a
- 2:27:48value to compare it with a value from
- 2:27:50the switch statements. Let's add one.
- 2:27:53Afterward, we can add a colon character.
- 2:27:56Code written after this character will
- 2:27:58be executed whenever the value from this
- 2:28:00case matches the value passed into the
- 2:28:03switch statement. Let's call the
- 2:28:05variable dump function with the
- 2:28:07following value success.
- 2:28:11If the values match, this means the
- 2:28:13payment was a success. Let's add another
- 2:28:16case. This time, we'll compare the
- 2:28:18variable with the number two. If the
- 2:28:21values match, we'll call the variable
- 2:28:23dump function with the following value,
- 2:28:26pending.
- 2:28:29You'll notice I'm indenting the code.
- 2:28:31This is not necessary, but recommended.
- 2:28:33It helps with readability. Indenting
- 2:28:36your code helps associate lines of code
- 2:28:38with a specific case. What if the
- 2:28:40variable doesn't match our cases? We can
- 2:28:43add a default case by adding the default
- 2:28:46keyword.
- 2:28:48Similar to before, we can add a colon
- 2:28:51character after this keyword. Any code
- 2:28:54written after this character will be
- 2:28:56executed when the other cases fail.
- 2:28:58Let's output the following message.
- 2:29:01Unknown.
- 2:29:03To recap, switch statements attempt to
- 2:29:06compare values for equality. It doesn't
- 2:29:09try to compare values that are less than
- 2:29:11or greater than each other. Within the
- 2:29:14switch statement, we must provide a
- 2:29:16value to match within its various cases.
- 2:29:18We can add as many case statements as
- 2:29:21we'd like. If a match can't be found,
- 2:29:23the default case is executed. Here's
- 2:29:26where things become slightly bizarre.
- 2:29:28All messages from our cases have been
- 2:29:30executed. But why? Whenever PHP comes
- 2:29:33across a match with a case, it'll
- 2:29:36execute all lines of code written after
- 2:29:38it. This includes lines of code written
- 2:29:41after the case. PHP refers to this
- 2:29:44feature as a fall through strategy.
- 2:29:46We're responsible for instructing PHP to
- 2:29:49start running code. This process can be
- 2:29:52interrupted by adding a keyword called
- 2:29:54break. After each variable dump
- 2:29:56function, add this keyword.
- 2:30:00We don't have to add the break keyword
- 2:30:02to the default case. It's redundant to
- 2:30:05add it in since it's the end of the
- 2:30:07switch statement. After adding this
- 2:30:09keyword, things are different. Only the
- 2:30:11success message appears. I know this
- 2:30:14might seem strange, but there's a good
- 2:30:16reason for this behavior. Currently,
- 2:30:18we're limited to matching a single value
- 2:30:20with the expression from the switch
- 2:30:22statement. What if we want multiple
- 2:30:24values to trigger a block of code?
- 2:30:27That's completely possible thanks to the
- 2:30:29fall through strategy implemented in
- 2:30:31switch statements. For example, after
- 2:30:33the second case, add another case for
- 2:30:36the number three.
- 2:30:39Next, let's update the payment status
- 2:30:41variable to two.
- 2:30:45As you can see, the payment was denied.
- 2:30:47Under the second case, there isn't any
- 2:30:50code to execute. However, we're not
- 2:30:52breaking the case either. PHP continues
- 2:30:55to fall through the next line of code.
- 2:30:57By not doing so, the message from the
- 2:30:59third case gets executed. This behavior
- 2:31:02allows us to add multiple cases to
- 2:31:05produce the same outcome, which may come
- 2:31:07in handy. In my opinion, I prefer to use
- 2:31:10if statements. It's completely possible
- 2:31:12to compare two values. However, from
- 2:31:15time to time, you may come across switch
- 2:31:17statements. It's good to know how they
- 2:31:19work. Before moving on, there's one more
- 2:31:22thing I want to mention. The types don't
- 2:31:24have to match. For example, I'll wrap
- 2:31:27the variable's value with a pair of
- 2:31:29quotes to create a string.
- 2:31:32Believe it or not, our code will work as
- 2:31:35before. The same message gets outputed.
- 2:31:38PHP performs type juggling before
- 2:31:40comparing values. The values do not have
- 2:31:43to be the same type. As long as they
- 2:31:45loosely match, the case will pass.
- 2:31:48That's something you should know when
- 2:31:49working with switch statements. If you
- 2:31:52require strict comparisons, you might
- 2:31:54want to consider using if statements. It
- 2:31:57isn't possible with switch statements.
- 2:31:59In the next lecture, let's look at one
- 2:32:01more feature of performing conditions.
- 2:32:04[Music]
- 2:32:14In this lecture, we're going to explore
- 2:32:16a feature of PHP called match
- 2:32:18expressions. This feature is relatively
- 2:32:21new with version 8 of PHP. If you're on
- 2:32:24an older version of PHP, this feature
- 2:32:26will not be available to you. So, what
- 2:32:29are match expressions? A match
- 2:32:31expression is similar to a switch
- 2:32:33statement. It's a feature that compares
- 2:32:35two values. The main difference is that
- 2:32:37the match keyword is treated as an
- 2:32:40expression. Let me give an example. In
- 2:32:42our script, we added a switch statement.
- 2:32:45Let's wrap the switch statement with the
- 2:32:47variable dump function.
- 2:32:51Almost immediately an error gets thrown
- 2:32:53by PHP. According to the error, the
- 2:32:56switch statement is unexpected. This is
- 2:32:59because the switch statement is not an
- 2:33:01expression. A value does not get
- 2:33:03produced by switch statements.
- 2:33:05Understanding this behavior is crucial.
- 2:33:08Not all lines of code produce values.
- 2:33:10Technically, the switch statement can
- 2:33:12contain expressions, but the switch
- 2:33:14statement itself is not an expression.
- 2:33:17The variable dump function wants an
- 2:33:20expression, otherwise it won't be able
- 2:33:22to output the value into the page. Let's
- 2:33:25switch the keywords from switch to
- 2:33:27match.
- 2:33:30Unlike the switch statement, a match
- 2:33:33expression produces a value. Match
- 2:33:35expressions can be written with the
- 2:33:37match keyword. Inside the parenthesis,
- 2:33:40we may provide a value to compare with
- 2:33:42the values written inside the curly
- 2:33:45brackets. I'm fine sticking with the
- 2:33:47payment status variable. I'll leave it
- 2:33:49as is. Inside the match expression, the
- 2:33:52syntax for adding a list of values is
- 2:33:55slightly different. We don't have to use
- 2:33:57the case keyword. Match expressions use
- 2:34:00a similar syntax to associative arrays.
- 2:34:03For example, we can write the following
- 2:34:05one equals greater than success.
- 2:34:10The value on the left is the value to
- 2:34:12compare with the value passed into the
- 2:34:14match expression. If the values match,
- 2:34:17the value on the right is returned as
- 2:34:19the result of the expression. Multiple
- 2:34:21values can be added by separating them
- 2:34:23with commas. Let's add the number two
- 2:34:26with the following result. Denied.
- 2:34:31Before we test our work, I think it
- 2:34:33would be a good idea to extract the
- 2:34:35expression. It may be difficult to read.
- 2:34:37Values produced by match expressions can
- 2:34:39be stored in a variable. After all, it's
- 2:34:42an expression. I'm going to extract the
- 2:34:44expression to a variable called message.
- 2:34:49Next, I'll pass this variable onto the
- 2:34:52variable dump function.
- 2:34:56After running the script, we were given
- 2:34:58an error. Let's dissect the error to
- 2:35:00understand the problem. The error states
- 2:35:03the following. Uncut unhandled match
- 2:35:06error. unhandled match case. Match
- 2:35:09expressions must always produce a value.
- 2:35:11If we look at our logic, the value
- 2:35:13stored in the variable is a string.
- 2:35:15However, the values in the expressions
- 2:35:17are integers. Unlike switch statements,
- 2:35:20PHP does not perform type juggling on
- 2:35:23any of the values. Comparisons are
- 2:35:25strict. This means that the values must
- 2:35:28match and have matching data types. With
- 2:35:31that in mind, it makes sense why we're
- 2:35:33getting an error. PHP can't find a
- 2:35:35match. In these cases, we must provide a
- 2:35:38default value in the expression. Add the
- 2:35:40default keyword with the following
- 2:35:43value, unknown.
- 2:35:46This time, the error goes away. Instead,
- 2:35:49we're given the default value. That
- 2:35:51makes sense since the data types don't
- 2:35:53match. Let's remove the quotes from the
- 2:35:56variables value.
- 2:35:59After doing so, let's run the script.
- 2:36:02Our
- 2:36:04payment has been denied. Match
- 2:36:06expressions can be easier to read than
- 2:36:08switch and if statements.
- 2:36:12However, you may not like match
- 2:36:14expressions. Here's an example of the
- 2:36:16same result, but with an if statement.
- 2:36:18In programming, there isn't always one
- 2:36:21solution for producing the desired
- 2:36:22results. Different solutions can be used
- 2:36:25to tackle the same problem. Some
- 2:36:27developers may not like match
- 2:36:29expressions. In that case, you can stick
- 2:36:31to if statements. Throughout this
- 2:36:33course, there will be examples of how to
- 2:36:36use features in practical scenarios. For
- 2:36:38now, let's keep moving on with our PHP
- 2:36:41journey. In the next lecture, let's move
- 2:36:43on to functions.
- 2:36:45[Music]
- 2:36:55In this lecture, we're going to start
- 2:36:57writing reusable code by using
- 2:36:59functions. So far, we've been using the
- 2:37:02variable dump function to output data
- 2:37:05into the page. It's a function defined
- 2:37:07by the PHP language. PHP has dozens of
- 2:37:10functions. However, we're not limited to
- 2:37:13PHP's functions. Custom functions can be
- 2:37:16defined by us. Let's look at an example.
- 2:37:18In our script, we've written a chunk of
- 2:37:21code to generate the status of a payment
- 2:37:23based on a number. This solution works,
- 2:37:26but it could be improved further. We may
- 2:37:28want to generate the payment status for
- 2:37:30various places in an application. For
- 2:37:33example, we may want to output this
- 2:37:35information on a dashboard. If the buyer
- 2:37:38wants an invoice, this information could
- 2:37:40be rendered in a PDF file. Lastly, we
- 2:37:43may want to display this information
- 2:37:45after a buyer submits payment
- 2:37:47information. In total, we have three
- 2:37:50different locations where this
- 2:37:52information could appear. As it stands,
- 2:37:55we would have to copy and paste this
- 2:37:57code for the information to be
- 2:37:58displayed. It would be convenient if we
- 2:38:01could write the code once and use it
- 2:38:03anywhere. That's the purpose of a
- 2:38:04function. Functions allow us to write
- 2:38:07logic once. This logic can be repeatedly
- 2:38:10executed anywhere in our codebase. Let's
- 2:38:13try defining a custom function. The goal
- 2:38:16of this function will be to provide the
- 2:38:18status of a payment. Above the current
- 2:38:20code, write the following function get
- 2:38:23status.
- 2:38:26Custom functions can be written by
- 2:38:28typing the function keyword. This
- 2:38:30keyword is followed by the name of the
- 2:38:32function. Function names follow the same
- 2:38:34rules as variable names. Unlike variable
- 2:38:37names, we don't have to start the name
- 2:38:40with a dollar sign character. It's
- 2:38:42common practice to use camel casing for
- 2:38:44function names. After the name, we can
- 2:38:47add a pair of parentheses and curly
- 2:38:49brackets.
- 2:38:52We're going to ignore the parenthesis.
- 2:38:54It's something we'll circle back to.
- 2:38:56Inside the curly brackets, we can write
- 2:38:58the body of the function. Let's move our
- 2:39:00logic into the function.
- 2:39:04Similar to before, we're indenting the
- 2:39:07code for readability. This is always
- 2:39:09true for code written in curly brackets.
- 2:39:12Next, we must invoke the function.
- 2:39:14Invoking a function is another way of
- 2:39:16saying running the code from within the
- 2:39:19body of a function. By default, PHP does
- 2:39:22not execute code inside a function. We
- 2:39:25must explicitly invoke the code. Oftent
- 2:39:28times you may hear developers say call a
- 2:39:30function or run a function. It means the
- 2:39:33same thing. A function can be called by
- 2:39:36typing the name of the function followed
- 2:39:38by a pair of parenthesis. Let's call the
- 2:39:41get status function after defining it.
- 2:39:46After making those changes, the same
- 2:39:48output has been rendered. That's
- 2:39:50perfect. We've successfully defined a
- 2:39:53custom function. This function can be
- 2:39:55called an unlimited number of times.
- 2:39:57There are a few things worth mentioning.
- 2:40:00Firstly, the function can be defined
- 2:40:02anywhere. At the moment, we've defined
- 2:40:05the function first and called it
- 2:40:07afterward. In this instance, the order
- 2:40:09does not matter. We can move the
- 2:40:11function definition below the invocation
- 2:40:14like so.
- 2:40:17The same result occurs. As long as the
- 2:40:19function is defined without
- 2:40:21interference, our code should work. So
- 2:40:24what do I mean by interference? Let's
- 2:40:26wrap our function with an if statement.
- 2:40:32For the condition, use the following
- 2:40:34value. true.
- 2:40:38As you might guess, the code inside the
- 2:40:40conditional statement will always
- 2:40:42execute. However, that doesn't mean the
- 2:40:44function is immediately available like
- 2:40:46before. An undefined error gets produced
- 2:40:49from our script. Functions inside
- 2:40:51conditional statements are not
- 2:40:53immediately available. The conditional
- 2:40:55statement must execute before the
- 2:40:57function is defined. However, we can
- 2:41:00move the conditional statement above the
- 2:41:02invocation to make the code work.
- 2:41:06If we run the script again, things
- 2:41:08should work.
- 2:41:12Everything is back to normal. This can
- 2:41:14be a gotcha with PHP. I always recommend
- 2:41:17defining your functions before executing
- 2:41:19additional logic. By doing so, your
- 2:41:22functions should be readily available.
- 2:41:24It's a common practice among developers.
- 2:41:26I'm going to remove the conditional
- 2:41:28statement wrapped around the function.
- 2:41:33There's one last thing I want to
- 2:41:34mention. Functions cannot be defined
- 2:41:37twice. They're like constants. If we
- 2:41:39attempt to define a function twice, PHP
- 2:41:42will complain. That's something to be
- 2:41:44aware of. In the next lecture, let's
- 2:41:46keep exploring additional features of
- 2:41:49functions.
- 2:41:50[Music]
- 2:42:00In this lecture, we're going to improve
- 2:42:02our function by using parameters. Let's
- 2:42:05take a moment to understand our
- 2:42:07function. While our solution works, it
- 2:42:09can be improved. The biggest issue with
- 2:42:12our function is the message. No matter
- 2:42:14how many times the function is called,
- 2:42:16the same message appears on the page.
- 2:42:19Our function should be flexible. This is
- 2:42:21possible with a feature called
- 2:42:23parameters. A parameter is a variable
- 2:42:26that can be updated outside the
- 2:42:28function. Customizing the variables in a
- 2:42:30function adds flexibility. Inside our
- 2:42:33function, remove the payment status
- 2:42:35variable.
- 2:42:38Next, inside the parentheses of the
- 2:42:40function, add a variable called payment
- 2:42:43status.
- 2:42:46Parameters can be added inside the
- 2:42:48functions parenthesis. Unlike variables,
- 2:42:51values do not need to be assigned. After
- 2:42:54adding this parameter, our script will
- 2:42:56break. In the preview, the error states
- 2:42:59that there are too few arguments. It's
- 2:43:01not the responsibility of the function
- 2:43:03to assign a value to the payment status
- 2:43:06parameter. Instead, the value must be
- 2:43:09provided when the function is called.
- 2:43:11This is why we're getting an error.
- 2:43:13We're not providing a value. A value can
- 2:43:15be sent to the function by writing it in
- 2:43:18parenthesis. Let's pass in one. This
- 2:43:21value will be the value of the payment
- 2:43:24status variable. Let's try testing our
- 2:43:26code by running the script.
- 2:43:30Everything is functioning as before. By
- 2:43:33using parameters, we can avoid hard
- 2:43:35coding a value into the function.
- 2:43:37Functions can be fed data to alter the
- 2:43:40output. We're not limited to a single
- 2:43:42parameter. Multiple parameters can be
- 2:43:44added by separating them with a comma.
- 2:43:47For example, let's add a parameter
- 2:43:49called show message.
- 2:43:53Let's use this parameter to toggle the
- 2:43:55variable dump function. We may not
- 2:43:58always want to output the payment status
- 2:44:00into the console. Wrap the function with
- 2:44:02an if statement. The condition will be
- 2:44:05the show message parameter.
- 2:44:09Next, in the invocation of the get
- 2:44:11status message, let's pass in true.
- 2:44:16The values passed into the function must
- 2:44:19correspond with the parameter list. The
- 2:44:21number will be assigned to the payment
- 2:44:23status parameter. The boolean will be
- 2:44:26assigned to the show message parameter.
- 2:44:28If we reverse these values, the values
- 2:44:31for the parameters will change. The
- 2:44:33order does matter. In some cases, you
- 2:44:36may want to assign default values to
- 2:44:38your parameters. What if we had five
- 2:44:41parameters? Constantly passing in five
- 2:44:44values to a function can be annoying.
- 2:44:46It's optional to add default values for
- 2:44:48a parameter. We can do so from the
- 2:44:51parameter list. Let's set the show
- 2:44:54message parameter to true.
- 2:44:58Next, we can remove the true value from
- 2:45:01the invocation.
- 2:45:04PHP won't throw an error if we don't
- 2:45:07pass on a value for a specific
- 2:45:09parameter. PHP checks the parameter for
- 2:45:12a default value. If there is a default
- 2:45:14value, it'll be used. Values passed into
- 2:45:17a function will override the default
- 2:45:19value of a parameter. We're getting the
- 2:45:22same output as before. That's awesome.
- 2:45:24There's one last thing I want to mention
- 2:45:26before moving on. In the programming
- 2:45:28world, you're going to hear two words
- 2:45:31called parameters and arguments. Often
- 2:45:34times, developers use these words
- 2:45:36interchangeably, but there's a
- 2:45:38difference. A parameter refers to the
- 2:45:41variable in the functions parameter
- 2:45:43list. Whereas an argument refers to the
- 2:45:46value of the parameter. In our case, the
- 2:45:48payment status and show message
- 2:45:51variables are parameters. No one's going
- 2:45:54to yell at you for using the wrong term.
- 2:45:56However, it's always good to know the
- 2:45:58difference. Throughout the course,
- 2:46:00you'll see me using both terms to
- 2:46:02describe a functions parameters. In the
- 2:46:04next lecture, let's continue improving
- 2:46:06our function by exploring return values.
- 2:46:10[Music]
- 2:46:20In this lecture, we're going to expose
- 2:46:22data from a function to the outside
- 2:46:24world. The get status function takes in
- 2:46:27a value. Based on this value, it'll
- 2:46:29produce a readable message for the
- 2:46:31payment status. This information is not
- 2:46:33available outside of the function.
- 2:46:35Exposing information outside of a
- 2:46:38function is a common practice for
- 2:46:39developers. What if we want to change
- 2:46:42the language of the text? Alternatively,
- 2:46:44we may want to change the formatting by
- 2:46:47wrapping the text with HTML tags. Not
- 2:46:50all pages may render text the same. For
- 2:46:52the most flexibility, we should expose
- 2:46:55the payment status to outside sources.
- 2:46:58This behavior is possible by returning
- 2:47:00the value. At the bottom of the
- 2:47:02function, add the return keyword
- 2:47:04followed by the message variable.
- 2:47:08By returning the value from the
- 2:47:10function, a few things happen. Firstly,
- 2:47:13the code inside the function stops
- 2:47:15executing. If we were to continue
- 2:47:17writing code after the return statement,
- 2:47:20nothing would happen. PHP won't execute
- 2:47:23the code written after this statement.
- 2:47:25This is why we've written the statement
- 2:47:27at the end of the function. Now the
- 2:47:29question is how do we access this
- 2:47:31information? Returning a value from a
- 2:47:34function can be stored when it's called.
- 2:47:36Below our function, we're invoking the
- 2:47:38get status function. By returning a
- 2:47:40value, we can store this information in
- 2:47:43a variable. Let's set a variable called
- 2:47:46status message to the functions return
- 2:47:48value.
- 2:47:51Next, call the variable dump function
- 2:47:54with the status message variable.
- 2:47:58The message gets rendered twice, once
- 2:48:01from inside the function and another
- 2:48:03time from outside the function.
- 2:48:05Returning data is optional, but it can
- 2:48:07be useful to use data from a function
- 2:48:09after it's been executed. Not all
- 2:48:12functions need to return data. It's
- 2:48:14completely optional. The variable dump
- 2:48:16function is an example of a function
- 2:48:19that doesn't return data. Overall,
- 2:48:22functions are a powerful feature for
- 2:48:24reusing logic. In a few lectures, we've
- 2:48:27created a function that can convert a
- 2:48:29status from a number to a human readable
- 2:48:31message. It's very flexible and gives us
- 2:48:34the power to customize its behavior.
- 2:48:37Functions have so much more to offer.
- 2:48:39For the next set of lectures, we're
- 2:48:41going to explore these features. I'll
- 2:48:43see you in the next one.
- 2:48:45[Music]
- 2:48:55In this lecture, we're going to apply
- 2:48:57type hinting to our function. At the
- 2:49:00moment, our function returns a string.
- 2:49:02PHP gives us the flexibility to return
- 2:49:05any type of value. It doesn't know the
- 2:49:07return type of our functions. It guesses
- 2:49:09the type during runtime. It's entirely
- 2:49:12possible to inform PHP of the return
- 2:49:15type of our function. There are many
- 2:49:17benefits to doing so. The main advantage
- 2:49:19is debugging an application. We've
- 2:49:22talked about this before. PHP is a
- 2:49:24dynamically typed language. It's capable
- 2:49:27of changing the data type of a variable
- 2:49:29at a moment's notice. We do not need to
- 2:49:32set the types of our variables. While
- 2:49:34convenient, that doesn't mean there
- 2:49:36aren't drawbacks. It's possible to use
- 2:49:38the wrong data type for a specific
- 2:49:40situation. Rather than deal with the
- 2:49:43headaches of a dynamically typed
- 2:49:44language, we can enforce types. By doing
- 2:49:47so, if we attempt to use a type
- 2:49:49incompatible with our code, we can be
- 2:49:52informed of the problem before testing
- 2:49:53our code. It's an incredibly valuable
- 2:49:56feature that'll save us hours of
- 2:49:58debugging. Return types can be enforced
- 2:50:00by adding a colon character after the
- 2:50:03parenthesis. After this character, we
- 2:50:05can specify a type. Let's use the string
- 2:50:08type.
- 2:50:11Adding this type can help PHP understand
- 2:50:14the type of value returned by the
- 2:50:16function. In some cases, we may not
- 2:50:18always return a type. For example, in
- 2:50:21our function, we're conditionally
- 2:50:23dumping the message. I don't think we
- 2:50:25should return the message from the
- 2:50:26function if the show message parameter
- 2:50:29is set to true. At the end of the
- 2:50:31conditional statement, let's return
- 2:50:33null.
- 2:50:36As you can imagine, returning a value
- 2:50:38causes the message to never be returned.
- 2:50:41This will only occur if the show message
- 2:50:43parameter is true. Returning a null will
- 2:50:46break our function. It's because we're
- 2:50:49returning null instead of a string. If
- 2:50:51we didn't have type hinting in our
- 2:50:53function, everything would be fine. In
- 2:50:55these cases, we can update our function
- 2:50:58by adding a question mark character
- 2:51:00before the return type.
- 2:51:04Adding this character allows a null
- 2:51:06value to be returned by the function.
- 2:51:08Thus, our function can return a string
- 2:51:11or null. In some cases, your function
- 2:51:14may not return anything at all. It's
- 2:51:16optional to return a value. If your
- 2:51:18function doesn't return a value, you can
- 2:51:21set the return type to void.
- 2:51:25The void data type is a special data
- 2:51:28type for functions. It was introduced
- 2:51:30for functions that never return data. In
- 2:51:33our case, we don't need this type.
- 2:51:35Regardless, it's good to know. Type
- 2:51:38hinting is not just available for return
- 2:51:40types. We can add type hinting to
- 2:51:43parameters. For example, let's add the
- 2:51:45integer type to the payment status
- 2:51:48parameter.
- 2:51:51Types must be added before the parameter
- 2:51:53name. Let's add the boolean type to the
- 2:51:56second parameter. We're not limited to a
- 2:51:59single type. PHP supports union types.
- 2:52:02Unions types are for variables that
- 2:52:04support different data types. For
- 2:52:07example, the status variable accepts a
- 2:52:10number. What if we want to accept the
- 2:52:12float data type? It's not uncommon to
- 2:52:15support integers and floats. An
- 2:52:17additional type can be supported by
- 2:52:19adding the pipe character followed by
- 2:52:21the new type. Let's add the float type.
- 2:52:26We can add as many types as we'd like.
- 2:52:29Alternatively, you can use a type called
- 2:52:31mixed.
- 2:52:34You may have parameters that accept all
- 2:52:37data types. Rather than listing each
- 2:52:39possible data type, you can use the
- 2:52:41mixed data type as a shortcut. This data
- 2:52:44type was designed to accept all data
- 2:52:47types for a parameter for our function.
- 2:52:49It's not necessary. I'll revert it to
- 2:52:52the previous union type.
- 2:52:56Type hinting is the power to explicitly
- 2:52:58set the data type of our parameters and
- 2:53:01return values. Enforcing data types
- 2:53:03reduces the likelihood of errors.
- 2:53:06However, we're not fully benefiting from
- 2:53:08type hinting. In the next lecture, let's
- 2:53:10discuss why this is the case.
- 2:53:13[Music]
- 2:53:23In this lecture, we're going to enable
- 2:53:25strict typing for our files. At the
- 2:53:28moment, our function has type hinting.
- 2:53:30However, type hinting isn't enforced.
- 2:53:33For example, I'm going to update the
- 2:53:35argument passed into the get status
- 2:53:37function by wrapping it with quotes.
- 2:53:42After doing so, nothing happens in the
- 2:53:44preview. Everything is perfectly
- 2:53:46functional. Things become more bizarre
- 2:53:48when we look at the results. As we know,
- 2:53:51match expressions compare values based
- 2:53:53on the value and data type. In our
- 2:53:56expression, we're using integers, but
- 2:53:58we're passing in a string. Regardless,
- 2:54:00it was able to find a match. This
- 2:54:02behavior occurs because PHP is
- 2:54:05performing type juggling. By adding type
- 2:54:07hinting, PHP casts the argument into the
- 2:54:11correct type before the block of code is
- 2:54:13executed. Therefore, our parameter is
- 2:54:15going to have the correct type. That's
- 2:54:18not exactly what we want. We shouldn't
- 2:54:20be passing in the wrong data type to
- 2:54:22begin with. A better approach would be
- 2:54:24to receive an error from PHP. This way,
- 2:54:27we can adjust the data type. Type
- 2:54:29juggling would be skipped entirely. PHP
- 2:54:32is more than capable of detecting types.
- 2:54:35However, this feature is not enabled. We
- 2:54:37must manually enable it from the top of
- 2:54:40the file. At the top of the file, add
- 2:54:42the following. Declare strict types
- 2:54:45equals 1.
- 2:54:48The declare keyword allows us to
- 2:54:50configure PHP's behavior. In this
- 2:54:53example, we're enabling the strict types
- 2:54:56option by setting it to one. After
- 2:54:58enabling this feature, the page throws
- 2:55:00an error. If I look at the error, the
- 2:55:03error states I'm using the string type
- 2:55:06when the function accepts a value with
- 2:55:08the integer or float type. PHP is not
- 2:55:11allowing our script to execute normally.
- 2:55:14Enabling strict mode prevents our script
- 2:55:16from passing in a value with the wrong
- 2:55:18data type. If we were to remove the
- 2:55:21quotes, the error should go away.
- 2:55:25By enabling strict mode, we're
- 2:55:27guaranteed to always be working with the
- 2:55:30correct data type. Weakly typed
- 2:55:32languages can introduce unintended
- 2:55:34behavior in your application. These
- 2:55:36types of bugs can be hard to fix. I
- 2:55:39always recommend enabling strict typing
- 2:55:41to avoid bugs. There is a catch. The
- 2:55:44declare statement is only applicable to
- 2:55:46the current file. If we had another PHP
- 2:55:49file, we must add the same statement to
- 2:55:52the file. Otherwise, strict mode won't
- 2:55:55be enabled. It can be annoying to type
- 2:55:57the same line of code, but it is worth
- 2:55:59the effort. In the next lecture, let's
- 2:56:02continue our PHP journey.
- 2:56:04[Music]
- 2:56:14In this lecture, we're going to discuss
- 2:56:16a small gotcha with logical operators. I
- 2:56:19know this might seem strange because we
- 2:56:21spent a lot of time talking about
- 2:56:23operators from the previous section.
- 2:56:25However, we couldn't discuss this
- 2:56:27feature until we talked about functions.
- 2:56:30Let's circle back to operators just for
- 2:56:32this lecture. PHP is lazy when it comes
- 2:56:35to conditional expressions. Believe it
- 2:56:37or not, it's completely possible for PHP
- 2:56:40to skip an expression when evaluating
- 2:56:42conditions with logical operators. Let
- 2:56:45me show you what I mean. In the PHP
- 2:56:47file, clear the contents of the file.
- 2:56:52Next, we're going to call the variable
- 2:56:54dump function with the following
- 2:56:56expression, true and true.
- 2:57:01PHP executes the expression in the order
- 2:57:04of the conditions. Firstly, it'll check
- 2:57:07if the first condition evaluates to
- 2:57:09true. If it does, the second expression
- 2:57:12is checked. Nothing complicated so far.
- 2:57:14But what if the first expression
- 2:57:16evaluates to false?
- 2:57:20In this case, it doesn't make sense for
- 2:57:22PHP to check the second condition. The
- 2:57:25entire expression will evaluate as false
- 2:57:27regardless of the value from the second
- 2:57:30condition. Both conditions must be true
- 2:57:32for the entire expression to return
- 2:57:34true. This behavior can potentially
- 2:57:37cause a problem. Define a custom
- 2:57:39function called example.
- 2:57:43In this function, let's echo a message
- 2:57:45to indicate the function called.
- 2:57:49Next, let's return true.
- 2:57:54Lastly, let's replace the second
- 2:57:56condition with an invocation of the
- 2:57:58example function.
- 2:58:01What do you think will happen? Do you
- 2:58:03think the message from the example
- 2:58:05function will appear on the page? Let's
- 2:58:07find out.
- 2:58:10Inside the page, only the variable has
- 2:58:13been logged. A message never gets echoed
- 2:58:15from our function. This is because PHP
- 2:58:18does not bother executing our function.
- 2:58:20Since the first condition was false, the
- 2:58:23entire expression was false. This
- 2:58:25behavior is beneficial. It prevents PHP
- 2:58:28from executing unnecessary logic.
- 2:58:31Applications should always be optimized
- 2:58:33for performance. This feature is known
- 2:58:35as shortcircuiting, which is common in
- 2:58:38most programming languages. Let's try
- 2:58:40updating the first condition to true.
- 2:58:45As you can see, the message from our
- 2:58:47function has been echoed. You should
- 2:58:49always be aware of this feature. If you
- 2:58:51have a function that's expensive to run,
- 2:58:53you may want to consider adding it to
- 2:58:55the end of a condition. This way, you
- 2:58:57don't waste precious resources. That's
- 2:59:00all I wanted to mention for operators.
- 2:59:02In the next lecture, let's get back into
- 2:59:05controlling logic flow.
- 2:59:07[Music]
- 2:59:18In this lecture, we're going to start
- 2:59:20exploring loops. Looping is the process
- 2:59:22of repeating the same actions. A loop
- 2:59:24can be performed with conditions or a
- 2:59:27collection of values. We're going to
- 2:59:29explore both options. To get started,
- 2:59:31we'll perform a loop based on a
- 2:59:33condition. In the script, I'm going to
- 2:59:35remove all the code.
- 2:59:39Next, let's create a variable called a
- 2:59:42with an initial value of one.
- 2:59:46This lecture will aim to output the
- 2:59:48numbers 1 through 15. This variable will
- 2:59:51be updated with each number. As we do
- 2:59:53so, we'll output the number into the
- 2:59:55page. This is the perfect use case for a
- 2:59:58while loop. A while loop allows us to
- 3:00:00repeat actions based on a condition.
- 3:00:03Let's look at an example. Below the
- 3:00:05variable, add the while keyword with a
- 3:00:08pair of parentheses and curly brackets.
- 3:00:13Inside the parenthesis, we must provide
- 3:00:15an expression that evaluates to a
- 3:00:17boolean value. If we don't provide a
- 3:00:20boolean value, PHP type juggles the
- 3:00:23value into a boolean. Expressions that
- 3:00:25evaluate to true will cause the code
- 3:00:27inside the curly brackets to execute.
- 3:00:30Otherwise, nothing happens. Let's use
- 3:00:32the following condition. A less than
- 3:00:35equals 15.
- 3:00:39In this example, the A variable must be
- 3:00:42less than or equal to the number 15.
- 3:00:44Since our variable stores the number
- 3:00:46one, this condition evaluates to true.
- 3:00:49Thus, our loop will be executed. PHP
- 3:00:52always checks the condition before
- 3:00:54executing the block of code. Inside our
- 3:00:57block of code, let's write the
- 3:00:59following. Echo A.Br.
- 3:01:04In this example, we're echoing the
- 3:01:06number along with the break tag. This is
- 3:01:08so that our numbers aren't cluttered.
- 3:01:10We're almost finished. At the moment, we
- 3:01:13have an infinite loop. Infinite loops
- 3:01:15are considered to be highly dangerous.
- 3:01:17After a block of code has been executed,
- 3:01:20PHP evaluates the expression written
- 3:01:22from within the parenthesis. Again, if
- 3:01:24the expression evaluates to true, the
- 3:01:27block of code is executed once more.
- 3:01:29This process repeats until the condition
- 3:01:32evaluates to false. A false value causes
- 3:01:35PHP to move on to the next line of code
- 3:01:38written after the loop. Our condition
- 3:01:41always evaluates to true. We are never
- 3:01:43updating the A variable. It's always
- 3:01:46considered good practice to avoid
- 3:01:48infinite loops. Otherwise, a block of
- 3:01:50code runs endlessly. Resources are
- 3:01:53consumed to the point that a system will
- 3:01:55crash. After echoing the value, let's
- 3:01:58write the following. A++.
- 3:02:02We could have used the addition operator
- 3:02:05to accomplish this task. However, it's
- 3:02:07not uncommon to increment a value by one
- 3:02:10from within a loop. For this reason, PHP
- 3:02:13introduces the increment operator to add
- 3:02:16one to a value. It's cleaner to read
- 3:02:18than the other solution. On each
- 3:02:20iteration, the number will increment.
- 3:02:23Eventually, the variable will be greater
- 3:02:25than 15. Thus, our loop stops running.
- 3:02:28The numbers 1 through 15 appear on the
- 3:02:31page. The while loop is a powerful
- 3:02:33feature. PHP has an alternative syntax
- 3:02:36for writing while loops called dowh.
- 3:02:39Let's convert our while loop into a
- 3:02:41dowhile loop. First, move the while
- 3:02:44portion of the code from the beginning
- 3:02:46to the end of the loop.
- 3:02:50Next, add a semicolon.
- 3:02:54Lastly, add the do keyword at the
- 3:02:56beginning of the loop.
- 3:02:59There's one major difference between a
- 3:03:01while and dowhile loop. A dowhile loop
- 3:03:04executes code from within the curly
- 3:03:06brackets at least once. This section of
- 3:03:09code is guaranteed to run once. After
- 3:03:11executing the block of code, PHP checks
- 3:03:14the conditions. Previously, the while
- 3:03:17loop would perform the condition check
- 3:03:19first. It's uncommon to use the dowhile
- 3:03:21loop, but it can be helpful when you
- 3:03:23need to run code at least once. In
- 3:03:25addition, a semicolon is necessary for
- 3:03:28the while keyword. Typically, PHP does
- 3:03:31not require the semicolon for blocks of
- 3:03:33code. However, since we're writing
- 3:03:35additional code after the closing
- 3:03:37bracket, we must manually end the
- 3:03:39statement. The same results are outputed
- 3:03:42into the page. That's to be expected
- 3:03:44since the condition doesn't change. To
- 3:03:46prove the block of code executes, let's
- 3:03:49change the condition by setting the
- 3:03:51number to zero.
- 3:03:54If we run the script again, only a
- 3:03:56single number appears despite the
- 3:03:58condition evaluating to false. PHP has
- 3:04:02executed our block of code before
- 3:04:04checking the condition. In most cases,
- 3:04:06you probably won't need the dowhile
- 3:04:08loop, but it is nice to know. In the
- 3:04:10next lecture, let's keep exploring
- 3:04:12loops.
- 3:04:14[Music]
- 3:04:25In this lecture, we're going to explore
- 3:04:27the for loop. The for loop was designed
- 3:04:30for repeating actions based on a
- 3:04:32condition. The code is going to be
- 3:04:33similar to the while loop. A few parts
- 3:04:36are going to be in different locations.
- 3:04:38Let's look at an example. In the PHP
- 3:04:41file, replace the contents of the file
- 3:04:43with the for keyword. This keyword is
- 3:04:46followed by a pair of parentheses and
- 3:04:48curly brackets.
- 3:04:51Inside the for keyword, we must provide
- 3:04:54three pieces of information. First, we
- 3:04:56must initialize a variable. Let's create
- 3:04:59a variable called I with an initial
- 3:05:01value of one.
- 3:05:04Previously, the while loop accepted a
- 3:05:07condition. Other pieces of information
- 3:05:09had to be managed in external areas. The
- 3:05:12for loop is different. Instead of being
- 3:05:14able to freely define variables outside
- 3:05:16of the loop, they can be written within
- 3:05:18the parenthesis. This adds more clarity
- 3:05:21to our loop. The second piece of
- 3:05:23information required by the loop is the
- 3:05:25condition. Let's use the same condition
- 3:05:27as before, which was the following. I
- 3:05:30less than equals 15.
- 3:05:34Lastly, we must provide an expression to
- 3:05:37increment the i variable. Let's use the
- 3:05:39increment operator.
- 3:05:43The syntax may feel strange. When PHP
- 3:05:46encounters a for loop, it'll execute the
- 3:05:48first portion of the loop, which is
- 3:05:50initializing a variable. The variable is
- 3:05:53never initialized again. It doesn't
- 3:05:55matter if the loop runs several times.
- 3:05:58This bit of code runs once. Next, PHP
- 3:06:02executes the second piece of code. If
- 3:06:04the expression evaluates to true, the
- 3:06:06block of code gets executed. Inside the
- 3:06:09block of code, let's echo the I variable
- 3:06:12with a break element.
- 3:06:15After the block of code has been
- 3:06:17executed, PHP executes the expression
- 3:06:20from the third section of the loop.
- 3:06:22After doing so, it'll execute the second
- 3:06:25condition. This process repeats until
- 3:06:27the condition evaluates to false.
- 3:06:30Essentially, we have the same thing as
- 3:06:32before. The main difference is that the
- 3:06:34logic has been centralized into a single
- 3:06:37location which may be easier to read on
- 3:06:40the page. The numbers 1 through 15 have
- 3:06:42been rendered. That's great. So why do
- 3:06:45we have two loops for performing the
- 3:06:47same thing? When would we use a while
- 3:06:50loop over a for loop? Vice versa, why
- 3:06:53would we use a for loop over a while
- 3:06:55loop? The for loop was introduced
- 3:06:58performing a loop based on a specific
- 3:07:00number. If you want to perform a loop 10
- 3:07:02times, you can tailor the expressions to
- 3:07:04this scenario. Whereas while loops are
- 3:07:07not restricted to a count, in some
- 3:07:09cases, we may want to perform a loop
- 3:07:11based on strings, arrays, or other types
- 3:07:14of values. We're not restricted to
- 3:07:16numbers. It's completely possible to
- 3:07:18stick to one loop. You don't have to use
- 3:07:21both loops in your programs. Most
- 3:07:23developers prefer the while loop. It's
- 3:07:26simpler than a for loop. Before moving
- 3:07:28on, there's one more thing I want to
- 3:07:30mention. PHP gives us the power to
- 3:07:32control loops with additional keywords.
- 3:07:35For example, we may want to skip a
- 3:07:37number. Inside the for loop, let's add
- 3:07:40an if statement.
- 3:07:43The condition will be the following. i=
- 3:07:476.
- 3:07:50It's perfectly valid to nest blocks of
- 3:07:53code. In this case, we're performing a
- 3:07:55condition before echoing the value. The
- 3:07:57equals equals equals operator is a
- 3:08:00comparison operator. It'll check if the
- 3:08:02values are equal to each other. If they
- 3:08:05are, we should skip the current
- 3:08:06iteration of the loop by using the
- 3:08:09continue keyword.
- 3:08:12The continue keyword can be used in any
- 3:08:14loop. This keyword instructs PHP to stop
- 3:08:18executing the block of code. It should
- 3:08:20immediately recheck the condition. You
- 3:08:22don't have to wait for the entire block
- 3:08:24of code to run. If you need to stop the
- 3:08:27current iteration from running, you can
- 3:08:29do so with the continue keyword. As you
- 3:08:31can see, the number six has been
- 3:08:33skipped. It does not appear in the
- 3:08:35output. Numbers after six appear. The
- 3:08:38continue keyword does not completely
- 3:08:40stop the loop. What if you want to end
- 3:08:42the loop completely? That's possible by
- 3:08:45replacing the continue keyword with the
- 3:08:48break keyword.
- 3:08:51By using the break keyword, the loop can
- 3:08:53be stopped earlier than intended. These
- 3:08:56keywords are not exclusive to the for
- 3:08:58loop. You can use them for any loop. In
- 3:09:00the next lecture, let's explore one more
- 3:09:03type of loop.
- 3:09:04[Music]
- 3:09:16In this lecture, we're going to explore
- 3:09:18one more feature for loops called a for
- 3:09:21each loop. So far, we've been performing
- 3:09:23a loop based on a condition. We're not
- 3:09:25limited to conditions. In some cases, we
- 3:09:28may need to loop through an array. It's
- 3:09:30a very common practice. PHP offers the
- 3:09:33for each loop for this specific purpose.
- 3:09:36Let's give it a try. First, let's create
- 3:09:38an array. In the script, clear the
- 3:09:41contents of the file.
- 3:09:44Next, define an array called names.
- 3:09:49Inside this array, add a list of random
- 3:09:51names.
- 3:09:54Lastly, add the for each keyword with a
- 3:09:57pair of parentheses and curly brackets.
- 3:10:02Our goal is to loop through each item in
- 3:10:04the array. During each iteration, we'll
- 3:10:06output the name. The for each keyword
- 3:10:09accepts a specific format. Type the
- 3:10:12following names as name.
- 3:10:16First, we must provide the array.
- 3:10:19Afterward, we can use the as keyword to
- 3:10:21create a reference for each item in the
- 3:10:23array. As PHP loops through the items in
- 3:10:26the array, the current item in the
- 3:10:28iteration will be stored in the variable
- 3:10:30after the as keyword. It's common
- 3:10:33practice to use the plural version of a
- 3:10:35word when defining arrays. Items in an
- 3:10:38array use the singular form of a word.
- 3:10:40You don't have to follow this practice,
- 3:10:42but it's commonly adopted among
- 3:10:44developers. PHP handles going through
- 3:10:47each item in the array. Inside the loop,
- 3:10:50let's call the variable dump function
- 3:10:52with the name variable.
- 3:10:56All three items from the array are
- 3:10:58listed. PHP goes through each item from
- 3:11:01first to last. Overall, the for each
- 3:11:04loop makes it easy to loop through items
- 3:11:06in the array. There are a few things I
- 3:11:09want to mention. Sometimes you may want
- 3:11:11to grab the key of the item in the
- 3:11:13array. It's completely optional to grab
- 3:11:16this information from the current
- 3:11:17iteration. In the parentheses, let's
- 3:11:20update the expression by adding the
- 3:11:22following after the as keyword. Key
- 3:11:25equals greater than.
- 3:11:28The syntax is similar to associative
- 3:11:31arrays. To the left of the arrow, we
- 3:11:33must provide a variable for storing the
- 3:11:36key of the item. You don't have to use
- 3:11:38these variable names. You can use
- 3:11:40whatever name you'd like. Generally,
- 3:11:42developers will use the word key for
- 3:11:44clarity. Inside our function, call the
- 3:11:47variable dump function with the key
- 3:11:50variable.
- 3:11:52Looking at the preview, the key is
- 3:11:55accessible via the variable. This
- 3:11:57concludes our discussion on loops. So
- 3:12:00far, we've explored four features for
- 3:12:02writing loops. PHP offers us the power
- 3:12:05to loop through an array or to write a
- 3:12:07condition to power the loop. Hopefully,
- 3:12:10it's clear why you would use each loop.
- 3:12:13[Music]
- 3:12:25In this lecture, we will explore a few
- 3:12:28of PHP's predefined constants. They may
- 3:12:31come in handy from time to time. In the
- 3:12:33resource section of this lecture, I
- 3:12:35provide a link to an official list of
- 3:12:37predefined constants. These constants
- 3:12:40are defined by the PHP language. You
- 3:12:43don't have to do anything to include
- 3:12:44them in your project. They are readily
- 3:12:46accessible. Let's go over some of them.
- 3:12:49Before we start using these constants,
- 3:12:51I'm going to clear the contents of the
- 3:12:53PHP file except for the opening PHP tag.
- 3:12:58All right, let's echo out a constant
- 3:13:01called PHP version.
- 3:13:04As you might suspect, this constant
- 3:13:07outputs the current PHP version on your
- 3:13:09server. This constant might come in
- 3:13:12handy if you're developing a script that
- 3:13:14will be deployed on multiple servers.
- 3:13:16Verifying the PHP version is a common
- 3:13:19action taken amongst PHP developers. If
- 3:13:22a server has an outdated PHP version,
- 3:13:25you can use this opportunity to notify
- 3:13:27an administrator to update the version.
- 3:13:30Older versions of PHP may have security
- 3:13:32bugs or flaws. In addition, you may be
- 3:13:35using a feature in PHP unavailable to
- 3:13:38previous versions. As you can see from
- 3:13:40the preview, I'm using PHP8. You may
- 3:13:43have a newer version depending on when
- 3:13:45you're watching this video. All right,
- 3:13:47let's try echoing a different constant
- 3:13:50called PHP int max.
- 3:13:55I'll concatenate a break element to echo
- 3:13:58another constant called PHP ins min.
- 3:14:03Technically, there's an infinite set of
- 3:14:05numbers. However, that doesn't mean that
- 3:14:08most computers are able to handle those
- 3:14:10numbers. In most cases, there's a
- 3:14:12maximum and minimum set of numbers that
- 3:14:14PHP can handle. Luckily, the range of
- 3:14:17numbers is extremely vast. It's uncommon
- 3:14:20to exceed these thresholds. If you're
- 3:14:22ever curious as to what those thresholds
- 3:14:24are, PHP stores the minimum and maximum
- 3:14:28energy values in those constants. The
- 3:14:30same goes for float values. For example,
- 3:14:33I'll replace the word integer with
- 3:14:35float.
- 3:14:38If we look at the preview, the format of
- 3:14:40the numbers may appear strange. There's
- 3:14:42a letter E in the middle of the number.
- 3:14:45PHP uses scientific notation for numbers
- 3:14:48that are too large to display. It's not
- 3:14:51something you'll ever have to worry
- 3:14:52about since most developers never exceed
- 3:14:55these limits. However, they're still
- 3:14:57good to know just in case. All right,
- 3:15:00those are some of the simpler constants
- 3:15:02offered by PHP. There's another subset
- 3:15:05of constants that are bizarre to say the
- 3:15:07least. In the resource section of this
- 3:15:09lecture, I provide a link to a page of
- 3:15:12magic constants. Magic constants defy
- 3:15:15the very concept of a constant. Unlike
- 3:15:18most constants, the value of a magic
- 3:15:20constant can change. Out of the box, PHP
- 3:15:23offers nine magic constants. So, what
- 3:15:26are they and why can they change? To
- 3:15:29understand how magic constants work,
- 3:15:31let's try using one. Back in our
- 3:15:33project, let's echo a constant called
- 3:15:36underscore
- 3:15:38line_.
- 3:15:42So, one thing worth noting about magic
- 3:15:45constants is that they all start and end
- 3:15:47with two underscores. This naming
- 3:15:49convention is how you can identify a
- 3:15:51magic constant. The line constant
- 3:15:54outputs the current line it's on, which
- 3:15:56can feel strange. However, it gets even
- 3:15:59more bizarre. If I were to add a few
- 3:16:01lines before the echo statement, the
- 3:16:03value can change.
- 3:16:06As you can see, the new value reflects
- 3:16:09the new line number. To take things a
- 3:16:11step further, we can output the constant
- 3:16:13again to get a completely different
- 3:16:15value. I'll add a break element to the
- 3:16:18first echo statement for readability.
- 3:16:22Once again, the correct line numbers get
- 3:16:24outputed, which is strange. The idea of
- 3:16:27a constant is that the value can never
- 3:16:30change. That's still partially true.
- 3:16:32Developers are not allowed to update
- 3:16:34constants. If we were to attempt to
- 3:16:37update a magic constant, PHP would throw
- 3:16:39an error. Only PHP is allowed to
- 3:16:42override the values of a magic constant.
- 3:16:45Magic constants are a feature for
- 3:16:47accessing information about the current
- 3:16:49file. Conveniently, PHP manages these
- 3:16:53variables on our behalf. What makes them
- 3:16:55unique is that their values can change
- 3:16:57based on where we use them. To give
- 3:17:00another example, I'm going to replace
- 3:17:02both constants with a constant called
- 3:17:05file and directory. Be sure to include
- 3:17:08two underscore characters before and
- 3:17:10after their names.
- 3:17:14The file constant contains the full
- 3:17:16system path to the current file. Notice
- 3:17:18how I said system path. It's not the
- 3:17:21HTTP URL to the file, but the path to
- 3:17:24the file on the current machine. Since
- 3:17:26we're using Replet, the path points to
- 3:17:28the file on Replet's servers. As for the
- 3:17:31directory constant, it points to the
- 3:17:34directory where the file can be found.
- 3:17:36Both constants are completely useful.
- 3:17:38More often than not, you're likely to
- 3:17:40use them a few times in each of your PHP
- 3:17:43applications. I highly recommend
- 3:17:45familiarizing yourself with them. That's
- 3:17:47about it for magic constants. In the
- 3:17:50next lecture, let's continue filling in
- 3:17:52the gaps of PHP.
- 3:17:54[Music]
- 3:18:05In this lecture, we're going to explore
- 3:18:08an alternative syntax for creating
- 3:18:10constants. PHP has two features for
- 3:18:13creating constants. So far, we've been
- 3:18:16using the const keyword. The other
- 3:18:18solution is to use the define function.
- 3:18:21Let's look at an example in our script.
- 3:18:23Let's replace the current code with a
- 3:18:25function called define.
- 3:18:29The define function has two parameters.
- 3:18:32The first parameter is the name of the
- 3:18:34constants. Unlike before, we must pass
- 3:18:36in the name as a string. Other than
- 3:18:38that, the same rules and conventions
- 3:18:41apply to constants. Let's set the name
- 3:18:43of the constant to fu.
- 3:18:47Next, the second parameter must contain
- 3:18:50the value of the constant. Let's set the
- 3:18:53value to hello world.
- 3:18:57Lastly, we can access the value of the
- 3:18:59constant by its name like any other
- 3:19:01constant. After defining the constant,
- 3:19:04let's echo the value
- 3:19:07in the preview. The value from the
- 3:19:09constant appears. That's great. But what
- 3:19:12are the differences between the define
- 3:19:14function and const keyword? Well, the
- 3:19:17const keyword was introduced for
- 3:19:19defining constants from within a class.
- 3:19:21Classes are a feature of PHP we haven't
- 3:19:24had the opportunity to discuss yet. They
- 3:19:26are going to be a prominent topic in an
- 3:19:29upcoming lecture. However, when we get
- 3:19:31to classes, the define function cannot
- 3:19:34be used from within a class, whereas the
- 3:19:36const keyword can. Another difference
- 3:19:39between both solutions is being able to
- 3:19:41use the define function from within a
- 3:19:44conditional statement. For example, I'm
- 3:19:46going to wrap the define function in an
- 3:19:49if statement.
- 3:19:52The condition for this statement will be
- 3:19:54true.
- 3:19:57If we run the script, PHP doesn't have a
- 3:20:00problem with our code. It's completely
- 3:20:02acceptable to define constants from
- 3:20:04within a conditional statement. However,
- 3:20:07this behavior is not possible with the
- 3:20:09const keyword. I'm going to quickly
- 3:20:12convert the constant from the define
- 3:20:14function to the const keyword.
- 3:20:18This time an error gets produced by PHP.
- 3:20:21If we use the const keyword, PHP is
- 3:20:24unable to create the constant from
- 3:20:26within a conditional statement. But the
- 3:20:29question is, why would we want to define
- 3:20:31a constant from within a conditional
- 3:20:33statement? The most common use case is
- 3:20:35to avoid redefining a constant. For
- 3:20:38example, I'm going to revert the
- 3:20:40constant definition to the define
- 3:20:42function
- 3:20:45afterward. Instead of passing in true,
- 3:20:48let's use a function called defined.
- 3:20:52The defined function is not to be
- 3:20:55confused with the defined function. They
- 3:20:57have similar names, but the function
- 3:20:59we're using has the letter D at the end.
- 3:21:02This function can be used to check that
- 3:21:04a constant has been defined. If you're
- 3:21:06developing a large application, you may
- 3:21:08have dozens of files. It's not uncommon
- 3:21:11to define the same constant from within
- 3:21:13multiple files. This is especially true
- 3:21:16if you have an application that can
- 3:21:18support plugins. In these cases, you may
- 3:21:21end up defining a constant twice. As we
- 3:21:23know, PHP does not allow a constant to
- 3:21:26be redefined after one has been created.
- 3:21:29To avoid errors, it's common practice to
- 3:21:32check that the constant has been
- 3:21:34created, which is what the defined
- 3:21:36function can do. This function accepts
- 3:21:38the name of the constant. Let's pass in
- 3:21:41fu.
- 3:21:44A true boolean value is returned by this
- 3:21:47function only if the constant has been
- 3:21:49defined. However, we want to check that
- 3:21:52the constant hasn't been defined because
- 3:21:54if it hasn't, we can go ahead with
- 3:21:56creating it. Luckily, we know an
- 3:21:58operator that can check for a false
- 3:22:00value. Do you remember what it is? I'll
- 3:22:03give you a moment to think about this.
- 3:22:07It's the not operator. We can add it to
- 3:22:10the beginning of the condition like so.
- 3:22:12Essentially, we're checking that the
- 3:22:14constant hasn't been created. If we look
- 3:22:17at the preview, everything is working as
- 3:22:19before. So, that's the define function.
- 3:22:22It's an alternative solution to using
- 3:22:24the const keyword. You can use whatever
- 3:22:26you prefer. In my personal preference, I
- 3:22:29like using the const keyword because
- 3:22:31it's easier to read. It's also easy to
- 3:22:34distinguish from the define function,
- 3:22:36which checks for a constant. However,
- 3:22:38you're more than welcome to continue
- 3:22:40using the define function. In the next
- 3:22:43lecture, let's move away from constants
- 3:22:45to talk about other features of PHP.
- 3:22:49[Music]
- 3:23:00In this lecture, we're going to learn
- 3:23:02about a function called unset. Whenever
- 3:23:05we define a variable, servers allocate
- 3:23:08memory for that variable. Memory is not
- 3:23:10released until the script has finished
- 3:23:13executing. However, we may want to free
- 3:23:15memory before that happens. PHP gives us
- 3:23:18the power to delete a variable after
- 3:23:20it's been created by using a function
- 3:23:23called unset. Let's give it a try. In
- 3:23:26our script, let's replace the current
- 3:23:28code with a variable called name. Its
- 3:23:30value will be John.
- 3:23:34Next, let's try echoing the variable.
- 3:23:39So far, nothing new. After echoing the
- 3:23:41variable, we may not need it anymore.
- 3:23:44That's going to be a common thing in
- 3:23:45your code. Sometimes you won't need your
- 3:23:48variables to stick around after they've
- 3:23:50served their use. We can unset the
- 3:23:52variable by calling the unset function
- 3:23:55with the variable passed in.
- 3:23:59Calling the unset function doesn't cause
- 3:24:02anything to happen on the screen. Behind
- 3:24:04the scenes, it's deleting the variable.
- 3:24:06To prove this, we can try echoing the
- 3:24:09name again.
- 3:24:12This time, we got an error from PHP.
- 3:24:14Despite defining it earlier, the
- 3:24:16variable becomes undefined because we've
- 3:24:19deleted it from memory. We're not
- 3:24:21limited to deleting an entire variable.
- 3:24:24What if we have an array? Individual
- 3:24:26items from an array can be deleted. For
- 3:24:28example, let's try updating the name
- 3:24:31variable by converting it into an array.
- 3:24:34Wrap the value with a pair of square
- 3:24:36brackets.
- 3:24:39Next, let's add an additional set of
- 3:24:41random names.
- 3:24:44Lastly, I'm going to update the variable
- 3:24:47name from name to names. Not a necessary
- 3:24:50step, but recommended. As I stated
- 3:24:52before, it's considered good practice to
- 3:24:55use the plural version of a word for
- 3:24:57variables that store arrays. After
- 3:25:00updating the variable, I'm going to
- 3:25:02update the next few lines of code to
- 3:25:04echo and unset the second item in the
- 3:25:06array.
- 3:25:09As a reminder, the second item of the
- 3:25:12array has an index of one. PHP starts
- 3:25:15counting at zero. Therefore, the second
- 3:25:17item's index is one. If we run the code,
- 3:25:20we get the same behavior as before. The
- 3:25:23name appears in the preview, gets unset,
- 3:25:25and then PHP throws an error because the
- 3:25:28item doesn't exist anymore. But what
- 3:25:31about the rest of the array? To find
- 3:25:32out, we should try rendering the entire
- 3:25:35array. One option is to use the variable
- 3:25:38dump function. On the other hand, I
- 3:25:40think this is a great opportunity to
- 3:25:42introduce you to another function for
- 3:25:44viewing the contents of the array.
- 3:25:46Replace the echo statements with a
- 3:25:48function called print r. While you're
- 3:25:50doing that, make sure that you're
- 3:25:52passing in the entire array instead of a
- 3:25:54specific item.
- 3:25:57The print r function was designed for
- 3:26:00outputting an entire array. The main
- 3:26:02difference between the print r function
- 3:26:04and variable dump function is that the
- 3:26:07print r function does not print the data
- 3:26:09types. There's less information in the
- 3:26:11output. Only the values appear in the
- 3:26:14preview. you're not always going to be
- 3:26:16interested in the data type of each item
- 3:26:18in the array. As you can see, the array
- 3:26:21continues to persist. Let's take a
- 3:26:23closer look at the array. The first
- 3:26:25array has three items, whereas the
- 3:26:27second array has two items. In the
- 3:26:29second array, the item with an index of
- 3:26:32one is completely missing. PHP goes from
- 3:26:35zero to two, skipping one. This confirms
- 3:26:38that the item was deleted. Now, with
- 3:26:41that being said, you may want to reindex
- 3:26:43the array. It can be awkward to have
- 3:26:45missing indexes. PHP does not
- 3:26:48automatically reindex an array after an
- 3:26:50item has been unset. We can manually
- 3:26:53reindex the array by updating the names
- 3:26:56variable to a function called array
- 3:26:59values.
- 3:27:02The array values function accepts the
- 3:27:04array to reindex. Let's pass that in.
- 3:27:09It'll return the reindexed array as a
- 3:27:12result. Looking at the preview, the
- 3:27:14second array is no longer missing the
- 3:27:16one index. The last item in the array
- 3:27:19got reindexed into the correct number.
- 3:27:21Overall, unsetting variables is
- 3:27:24incredibly easy. With one line of code,
- 3:27:26you can release memory from your script.
- 3:27:28In the next lecture, let's take a moment
- 3:27:30to talk about PHP's documentation.
- 3:27:34[Music]
- 3:27:46In this lecture, we're going to take a
- 3:27:48moment to talk about PHP's
- 3:27:50documentation. For the past few
- 3:27:52lectures, I've introduced a few
- 3:27:54functions to you, but didn't show you
- 3:27:56how I found them. As mentioned before,
- 3:27:58PHP has hundreds of functions. It's
- 3:28:01impossible to cover every single one.
- 3:28:03But how do you know which functions are
- 3:28:05worth remembering and which aren't? That
- 3:28:07isn't an easy question to answer. There
- 3:28:10isn't an official list of recommended
- 3:28:11functions to remember. However, not all
- 3:28:14hope is lost. Learning to read the
- 3:28:16documentation is one of the most crucial
- 3:28:18skills you can pick up as a developer.
- 3:28:21That's my goal for this lecture. In the
- 3:28:23resource section of this lecture, I
- 3:28:25provide a link to PHP's manual. On this
- 3:28:28page, PHP provides information on
- 3:28:31various topics from syntax to security
- 3:28:33practices. There's a wealth of knowledge
- 3:28:36to be found in the manual. One of the
- 3:28:38sections you may find yourself
- 3:28:40interested in is the functions
- 3:28:42reference. Search for it and click the
- 3:28:44link.
- 3:28:47The functions reference provides a
- 3:28:49complete list of functions available in
- 3:28:51PHP. You can perform various actions
- 3:28:54from interacting with a database to
- 3:28:56creating files on a server. I highly
- 3:28:59recommend bookmarking this page as you
- 3:29:01may need it in the future. Regardless of
- 3:29:03what function you plan on using, the
- 3:29:06structure of a function's documentation
- 3:29:08page is the same. Let's look at an
- 3:29:10example in the top right. Search for a
- 3:29:13function called array values.
- 3:29:17We learned about this function from the
- 3:29:19previous lecture. Before using a
- 3:29:21function, you should always take the
- 3:29:23time to view the documentation. But how
- 3:29:26do you read a function's documentation
- 3:29:27page? Let's try going through each
- 3:29:30section one by one. First and foremost,
- 3:29:33a list of supported PHP versions is
- 3:29:36provided under the function name. Not
- 3:29:38all functions are available in every
- 3:29:40version of PHP. Some functions have been
- 3:29:42around since the early days of PHP.
- 3:29:45Other functions were introduced in newer
- 3:29:47versions. If a function is not behaving
- 3:29:50as expected, you should check that it's
- 3:29:52available in your current version of
- 3:29:54PHP. Luckily, we're using the latest
- 3:29:56version of PHP, which is version 8. The
- 3:29:59array values function is available in
- 3:30:02this version. Below the version list,
- 3:30:04we're given a short description of the
- 3:30:06function. In my opinion, the most
- 3:30:09important portion of the description is
- 3:30:11the function definition. This portion of
- 3:30:14the description has two critical pieces
- 3:30:16of information. Firstly, the functions
- 3:30:19parameters are listed in the definition.
- 3:30:21Pay close attention to the data type. If
- 3:30:24you pass in the incorrect data type, PHP
- 3:30:27may attempt type juggling. Whenever
- 3:30:29possible, you should avoid that behavior
- 3:30:31by passing in the correct data type.
- 3:30:34Otherwise, you might encounter errors or
- 3:30:36unexpected behavior. The second critical
- 3:30:39piece of information is the return type,
- 3:30:41which is always written after the colon
- 3:30:43character in the function definition.
- 3:30:45According to the documentation, we can
- 3:30:48expect the function to return an array.
- 3:30:50It's a great summary, but we may require
- 3:30:53more information. Luckily, PHP has us
- 3:30:55covered. Below the description, the
- 3:30:58parameters and return values sections
- 3:31:01provide additional context. Honestly,
- 3:31:04there's not much to this function. It
- 3:31:06just accepts an array and returns a
- 3:31:08reindexed array. Regardless, you're most
- 3:31:11likely to check out these sections of
- 3:31:13the documentation page. Most functions
- 3:31:16have these sections. PHP is very good at
- 3:31:19being consistent. There are more
- 3:31:21sections worth looking at. Afterward,
- 3:31:23there's a section with an example. This
- 3:31:26section gives you the opportunity to
- 3:31:28look at how to use a function. Sometimes
- 3:31:30you may find multiple examples. In this
- 3:31:33case, we have a single example of
- 3:31:36reindexing an array. Moving along, we
- 3:31:39have a section called see. Also, you may
- 3:31:42come across a function that doesn't
- 3:31:44really solve the problem you're facing.
- 3:31:46If that's the case, you might want to
- 3:31:48check out this section of a functions
- 3:31:50documentation page. This section
- 3:31:52provides an alternative list of
- 3:31:54functions that you can use. Oftent
- 3:31:56times, they're similar to the function
- 3:31:59you're looking at currently. For
- 3:32:01example, PHP recommends the array keys
- 3:32:04function for grabbing an array of keys
- 3:32:06instead of the arrays values. If you
- 3:32:08need the keys of an array, this function
- 3:32:10is a great alternative. Let's keep
- 3:32:13moving forward. One of the last sections
- 3:32:15is user contributed notes. As mentioned
- 3:32:18before, PHP is an open-source language.
- 3:32:22Volunteers from all over the world
- 3:32:24contribute to PHP's growth and
- 3:32:26maintenance. Part of that effort is
- 3:32:28improving the documentation. This
- 3:32:30section has notes from other developers
- 3:32:32of PHP. Developers can freely submit
- 3:32:36additional pieces of information about a
- 3:32:38function that is missing from the
- 3:32:39current documentation. If you ever need
- 3:32:42more examples or information, this
- 3:32:44section is worth checking out. There's
- 3:32:46actually one more section that I want to
- 3:32:49show you that isn't available on this
- 3:32:51functions documentation page. We'll need
- 3:32:53to look at a different function. In the
- 3:32:55search bar, search for the define
- 3:32:57function.
- 3:33:00Like usual, we're going to encounter the
- 3:33:03same sections from the previous
- 3:33:04function. However, there's one
- 3:33:06additional section you may discover.
- 3:33:08Scrolling through the page, there's a
- 3:33:10section called change log. PHP is an
- 3:33:14evolving language. Not all functions
- 3:33:16remain the same across different
- 3:33:17versions. The PHP team may take the time
- 3:33:21to improve some functions. If a function
- 3:33:23changes, those changes are documented in
- 3:33:26this section. For example, since PHP 7,
- 3:33:30the define function allows developers to
- 3:33:33use arrays as values. Prior to this
- 3:33:35version, arrays were not supported in
- 3:33:38constants. This is important to
- 3:33:40understand. Just because a function
- 3:33:42works in one version doesn't mean it'll
- 3:33:44work the same way in another version.
- 3:33:47Occasionally, you may find yourself
- 3:33:49using a function that behaves
- 3:33:51differently in older projects. Checking
- 3:33:53out this section of the page can help
- 3:33:55you find out why. All right, so that's
- 3:33:58about it for the documentation of PHP.
- 3:34:01Throughout the course, I'll be sure to
- 3:34:03mention other helpful information worth
- 3:34:05looking at. For now, this information
- 3:34:07should suffice. Let's get back into
- 3:34:10programming in the following lecture.
- 3:34:14[Music]
- 3:34:24In this lecture, we will learn how to
- 3:34:26round numbers with PHP. Not only that,
- 3:34:29we're going to build upon our reading
- 3:34:31comprehension of PHP's documentation.
- 3:34:34Check out the resource section of this
- 3:34:36lecture for links to three functions.
- 3:34:39We're going to be exploring three
- 3:34:41functions called seal, floor, and round.
- 3:34:44Starting with the floor function, let's
- 3:34:47check out the documentation page. Most
- 3:34:49of the information is self-explanatory.
- 3:34:52However, there's one piece of
- 3:34:54information I want to highlight from the
- 3:34:56page. and the description. The data type
- 3:34:59for the number parameter is a union
- 3:35:01type. You can identify union types by
- 3:35:04the pipe character. As you can see from
- 3:35:06this example, the seal function accepts
- 3:35:08an integer or float. Just wanted to
- 3:35:11point this out as you may come across
- 3:35:14union types in the documentation. All
- 3:35:16right, let's try using this function.
- 3:35:19Switch over to your replet project in
- 3:35:21the script. We're going to echo the
- 3:35:23following number 5.454. 454.
- 3:35:29So far, we've learned that casting a
- 3:35:32float to an integer causes PHP to round
- 3:35:35the number. However, it's not truly
- 3:35:37rounding the number. It just strips out
- 3:35:39the decimal values. For example, let's
- 3:35:42try type casting the number into an
- 3:35:45integer like so.
- 3:35:48In the preview, we've given the number
- 3:35:50without the decimal values. But once
- 3:35:53again, PHP is not rounding the values.
- 3:35:56If we want to round the value, we can
- 3:35:58use one of three functions. One of the
- 3:36:01functions is called floor.
- 3:36:05This function always rounds a number
- 3:36:07down. As you can see, we get the same
- 3:36:09result. But what if the number was
- 3:36:11something like 5.9?
- 3:36:15Regardless, the number gets rounded
- 3:36:17down. Now, you may be thinking, how is
- 3:36:20this any different from type casting the
- 3:36:22value into an integer? It seems like we
- 3:36:24get the same results. However, that's
- 3:36:27not entirely true. PHP is attempting to
- 3:36:30round the number when we use the floor
- 3:36:32function. To prove this, let me show you
- 3:36:34a different example. Instead of echoing
- 3:36:37the floor function, let's type cast a
- 3:36:39negative number like minus0.1.
- 3:36:45Type casting this value into an integer
- 3:36:47produces zero. But what do you think
- 3:36:50would happen if we were to use the floor
- 3:36:52function instead? Let's find out.
- 3:36:56Upon replacing the type cast with the
- 3:36:59floor function, we get minus1. That
- 3:37:02makes sense, right? When we type cast a
- 3:37:04number, PHP strips the.1 decimal which
- 3:37:08leaves us with zero. I always encourage
- 3:37:11students to use these functions to round
- 3:37:14values instead of type casting the value
- 3:37:16as it'll always produce reliable
- 3:37:18results. Let's keep exploring the other
- 3:37:21rounding functions. Another function
- 3:37:23that I linked to was the seal function.
- 3:37:26This function behaves similarly to the
- 3:37:28floor function. It'll round a number,
- 3:37:31but the number will be rounded up. For
- 3:37:33example, let's try rounding a number
- 3:37:35like 5.454
- 3:37:37with the seal function.
- 3:37:41This time we got six. PHP gives us the
- 3:37:44option of rounding down or up. But what
- 3:37:46if we don't want to round to a whole
- 3:37:48number? Sometimes we may want to round
- 3:37:50with better precision. Well, it turns
- 3:37:52out there is such a function called
- 3:37:54round. Let's check out the documentation
- 3:37:56for this function. Unlike the other two
- 3:37:59functions, there are more parameters.
- 3:38:01The round function was introduced for
- 3:38:04customizing the rounding functionality
- 3:38:06offered by PHP. To understand how this
- 3:38:09function works, let's go through the
- 3:38:11documentation. In the description, the
- 3:38:14functions parameters have default
- 3:38:16values. Looking closely, the assignment
- 3:38:18operator is present in the definition.
- 3:38:21Whenever you see the assignment
- 3:38:23operator, the documentation is telling
- 3:38:25you two things. Firstly, the parameter
- 3:38:28is optional. Secondly, it tells you what
- 3:38:30the default value of that parameter is.
- 3:38:33The first parameter is the number to
- 3:38:36round. The second parameter is called
- 3:38:38precision with a default value of zero.
- 3:38:41The last parameter is called mode with a
- 3:38:43default value of constant. But how do I
- 3:38:46know it's a constant? I don't know for
- 3:38:48sure because we haven't read the
- 3:38:50description from the parameter section
- 3:38:52of the documentation. However, two signs
- 3:38:56lead me to believe this is a constant.
- 3:38:58Firstly, the value is written in all
- 3:39:01uppercase letters. As we know it's
- 3:39:03common for PHP developers to use all
- 3:39:06uppercase letters for constant names.
- 3:39:08This convention is followed by the PHP
- 3:39:11language itself. Secondly, the name of
- 3:39:13the constant starts with PHP which is
- 3:39:16commonly used by the PHP language. Just
- 3:39:19to make sure, let's read the
- 3:39:21descriptions of these parameters.
- 3:39:23According to the description of the
- 3:39:25precision parameter, this parameter
- 3:39:27allows us to configure the number of
- 3:39:29digits that can be present in the
- 3:39:31number. If we look back at the function
- 3:39:34definition, the precision is set to
- 3:39:36zero. Therefore, all decimal values are
- 3:39:38removed from the result. We can override
- 3:39:41this behavior by passing in a value for
- 3:39:43this parameter. Moving along, the mode
- 3:39:46parameter allows us to configure the
- 3:39:48rounding behavior. Should the number be
- 3:39:51rounded down or up when the number is at
- 3:39:53the halfway point? By default, the
- 3:39:55number goes up. We can change this
- 3:39:57behavior by using one of the constants
- 3:40:00listed on the documentation page. All
- 3:40:02right, so we've gotten an overview of
- 3:40:05this function. Let's try using it. Head
- 3:40:07back to replet. Currently, we're
- 3:40:09rounding the number with the seal
- 3:40:11function. Let's replace it with the
- 3:40:13round function.
- 3:40:16We get the number five in the preview.
- 3:40:19Despite having additional parameters,
- 3:40:21they are completely optional. Therefore,
- 3:40:23we can skip adding those into the
- 3:40:25function. When you're working with
- 3:40:27currencies, you may want to include
- 3:40:29decimals. For example, most currencies
- 3:40:32allow two digits in a decimal value. In
- 3:40:35this case, we have three. According to
- 3:40:38the documentation, the precision can be
- 3:40:40configured by passing in an integer with
- 3:40:42the exact number of decimals we'd like.
- 3:40:45Let's pass in two.
- 3:40:49Just like that, we've got a finetuned
- 3:40:52rounded value. Let's take it a step
- 3:40:54further by changing the number to 4.455.
- 3:41:00Naturally, PHP rounds the number to
- 3:41:034.46.
- 3:41:05But what if we want to round the number
- 3:41:06down when the last number is 5? That can
- 3:41:09be configured with the third parameter.
- 3:41:12We can pass in a constant called PHP
- 3:41:15round half down.
- 3:41:19This time the number changes to 4.45.
- 3:41:23So, as you can see, the round function
- 3:41:25is much more powerful than the floor or
- 3:41:28seal functions. I recommend using it
- 3:41:31whenever you need to round numbers with
- 3:41:32better precision. I know I spent a lot
- 3:41:35of time reading the documentation with
- 3:41:37you. However, it's an invaluable skill
- 3:41:39for a developer to understand how to
- 3:41:42read the documentation. You're not going
- 3:41:44to know every function in existence. In
- 3:41:47these cases, deciphering a function's
- 3:41:49documentation page will make you a
- 3:41:51better developer. In the next lecture,
- 3:41:53let's move on to conditional statements.
- 3:41:57[Music]
- 3:42:08In this lecture, we're going to look at
- 3:42:11an alternative syntax for writing
- 3:42:13conditional statements. PHP introduces a
- 3:42:16different solution for writing
- 3:42:18conditional statements for readability
- 3:42:20in our script. Let's say we wanted to
- 3:42:22welcome a user based on their
- 3:42:24permission. Let's replace the current
- 3:42:26code with a variable called permission.
- 3:42:29Set the value to one.
- 3:42:32I'll add a comment to describe this
- 3:42:34variable. If the number is one, the user
- 3:42:37is an administrator. If the number is
- 3:42:39two, they're a moderator. Any other
- 3:42:42number will be considered a guest.
- 3:42:46Below the variable, add an if statement
- 3:42:49with the following condition. Permission
- 3:42:51equals equals equals 1.
- 3:42:56Next, inside the block of code, let's
- 3:42:59echo an H1 tag that says the following.
- 3:43:02Hello admin.
- 3:43:05Echoing HTML is completely valid.
- 3:43:08Unfortunately, there's one huge flaw
- 3:43:10with doing so. It's difficult to read
- 3:43:12the string without syntax highlighting.
- 3:43:15Every character in the string is a
- 3:43:17single color. I really like syntax
- 3:43:19highlighting for readability. Luckily,
- 3:43:21we don't have to echo the element. An
- 3:43:24alternative solution is to exit and
- 3:43:26re-enter PHP mode. For example, I'll
- 3:43:29replace the echo keyword with a closing
- 3:43:32PHP tag.
- 3:43:35Next, after the closing H1 tag, I'll add
- 3:43:39an opening PHP tag.
- 3:43:43Unlike before, we get syntax
- 3:43:45highlighting. In my opinion, it's easier
- 3:43:47to read. Let's continue adding more
- 3:43:50conditions. Add an else if condition
- 3:43:53with the following condition. Permission
- 3:43:55equals equals 2.
- 3:44:00Next, let's output another H1 tag with
- 3:44:03the following message. Hello mod.
- 3:44:08Lastly, let's add an else block.
- 3:44:12This block outputs another H1 tag with
- 3:44:15the following message. Hello guest.
- 3:44:20So, we've got a working series of
- 3:44:22conditional statements. There's just one
- 3:44:24problem I have with it. Reading this
- 3:44:26code is slightly difficult. It's not
- 3:44:28uncommon to conditionally render HTML.
- 3:44:31For this reason, PHP has a different
- 3:44:33syntax for writing conditional
- 3:44:35statements. First, let's put the
- 3:44:38variable definition on a single line
- 3:44:40with opening and closing PHP tags.
- 3:44:45Next, let's replace our conditional
- 3:44:47statements. I'll add the if statement
- 3:44:50with the same condition. However, this
- 3:44:52time, instead of using curly brackets,
- 3:44:55I'll add a colon character after the
- 3:44:57parenthesis.
- 3:45:00Be sure to wrap the condition with
- 3:45:02opening and closing PHP tags. After
- 3:45:05these tags, add another pair of tags
- 3:45:08with a keyword called endiff.
- 3:45:12The alternative syntax doesn't require
- 3:45:15opening and closing curly brackets.
- 3:45:17Instead, we can denote the beginning of
- 3:45:19the block of code by using a colon
- 3:45:22character. The end of the block can be
- 3:45:24denoted with the end if keyword. In
- 3:45:26between these PHP tags, we can write our
- 3:45:29HTML. Let's greet the admin again.
- 3:45:34What about the else if and else blocks?
- 3:45:37It's the same as before. After the H1
- 3:45:40tag, add a pair of PHP tags with the
- 3:45:43elseif keyword.
- 3:45:46Two things worth noting. Firstly, the
- 3:45:49else condition is written before the end
- 3:45:51if keyword. This keyword acts as the
- 3:45:54ending for the entire chain of
- 3:45:56conditional statements, not just the if
- 3:45:58statement. If we introduce the else if
- 3:46:01keyword after the if keyword, PHP
- 3:46:03assumes this is the end of the first if
- 3:46:05keyword. Secondly, the else if keyword
- 3:46:08cannot be written with a space between
- 3:46:10the words. Previously, we have the
- 3:46:12option of adding a space between the
- 3:46:14else and if keywords, but this syntax
- 3:46:17does not support that option. They must
- 3:46:19be written as one word. All right, let's
- 3:46:22write the condition.
- 3:46:25Next, let's output an H1 tag to greet
- 3:46:29the moderator.
- 3:46:32Lastly, let's add the else block in
- 3:46:34another pair of PHP tags.
- 3:46:39Greet the guest.
- 3:46:42We have the same result as before. PHP
- 3:46:45introduces the syntax for readability.
- 3:46:48The conditional statements work the
- 3:46:50exact same. If you don't like this
- 3:46:52syntax, that's perfectly fine. The other
- 3:46:54solution works just as well. Generally,
- 3:46:57it's recommended to use curly brackets
- 3:46:59when you're only writing PHP code. If
- 3:47:02you need to write HTML, this syntax is
- 3:47:05considered to be superior for its
- 3:47:07readability. That's how most developers
- 3:47:09decide when to use which syntax. In the
- 3:47:12next lecture, let's talk about another
- 3:47:14topic related to conditional statements.
- 3:47:18[Music]
- 3:47:29In this lecture, we're going to talk
- 3:47:31about optimizing our code. It's never
- 3:47:34too early to talk about optimization.
- 3:47:36So, what exactly are we going to
- 3:47:38optimize? Not all functions are created
- 3:47:40equal. Some functions can be executed
- 3:47:43within an instant. Other functions may
- 3:47:45take a while to finish. You're never
- 3:47:47guaranteed how long your code will take
- 3:47:49to run. In these cases, we should take
- 3:47:52every opportunity to optimize our code.
- 3:47:55For example, let's say the user's
- 3:47:57permission was outsourced in a function.
- 3:48:00Replace the permission variable with a
- 3:48:02function called get permission.
- 3:48:06From within this function, return one.
- 3:48:10Lastly, let's update our conditions to
- 3:48:13use this function instead of the
- 3:48:15permission variable.
- 3:48:18So, what we're doing is returning the
- 3:48:20permission from the function. The same
- 3:48:22result occurs in our page. But let's say
- 3:48:25that this function takes a while to
- 3:48:27execute. Believe it or not, our code is
- 3:48:29inefficient. To find out why, let's try
- 3:48:32extending the duration of our function.
- 3:48:35Before returning a number, let's run a
- 3:48:37function called sleep.
- 3:48:41The sleep function instructs PHP to
- 3:48:43pause its execution. We can set the
- 3:48:46duration by passing in a number. Let's
- 3:48:48wait 2 seconds.
- 3:48:52After making that change, we can refresh
- 3:48:54the page. This time, the result takes a
- 3:48:57while to appear. Until 2 seconds have
- 3:48:59passed, the welcome message won't
- 3:49:01appear. So far, it isn't that bad.
- 3:49:04However, what if our function returned
- 3:49:06two instead of one?
- 3:49:10Things become even slower. Upon
- 3:49:12refreshing the page, we're going to be
- 3:49:14waiting 4 seconds. But why? Let's look
- 3:49:17at our code to understand the problem.
- 3:49:19In the if condition, PHP executes the
- 3:49:22get permission function. At this point,
- 3:49:24we're waiting 2 seconds. Once the
- 3:49:26function returns a value, PHP compares
- 3:49:29the numbers which don't match. PHP moves
- 3:49:32on to the next condition. Once again,
- 3:49:35we're calling the get permission
- 3:49:36function. Just like before, we must wait
- 3:49:392 seconds before the values can be
- 3:49:41compared. This is why our page is taking
- 3:49:44a while to load. Imagine if we had
- 3:49:46multiple conditions with the get
- 3:49:48permission function. Users would be
- 3:49:50waiting forever. It's perfectly fine to
- 3:49:52call functions from within your
- 3:49:54conditions. However, if you decide to do
- 3:49:57so, you should realize that PHP must
- 3:49:59wait for your function to finish
- 3:50:01executing before completing the
- 3:50:03comparison. Otherwise, your machine may
- 3:50:06occupy too many resources. There are two
- 3:50:09ways of resolving this issue. The first
- 3:50:12solution is to outsource the value
- 3:50:14returned by your function in a variable.
- 3:50:16Above the conditional statements, add a
- 3:50:19variable called permission with the get
- 3:50:21permission function as the value.
- 3:50:26Next, let's use the variable instead of
- 3:50:28the function.
- 3:50:31This is so much better. Now we're only
- 3:50:33executing the function once instead of
- 3:50:36for each condition. So what about the
- 3:50:39second solution? Instead of using if
- 3:50:41statements, we can use switch
- 3:50:43statements. I'm going to quickly convert
- 3:50:45this solution into a switch statement.
- 3:50:47You don't have to follow along. Just
- 3:50:49watch.
- 3:50:58In this solution, we're calling the get
- 3:51:00permission function from within the
- 3:51:02switch statements. I'm adding a case for
- 3:51:04each type of user followed by a message
- 3:51:07to greet the user. It's the same thing
- 3:51:09as before. Interestingly, the page does
- 3:51:12not take 4 seconds to load. Switch
- 3:51:15statements execute the expression passed
- 3:51:17into the switch statement once.
- 3:51:19Therefore, the get permission function
- 3:51:22runs once. If the first case fails, PHP
- 3:51:26does not execute the function again. It
- 3:51:28just uses the value from before to
- 3:51:30compare with the next case. We never
- 3:51:33have to worry about a function being
- 3:51:34called repeatedly, thus saving us the
- 3:51:37trouble of storing the value in a
- 3:51:39separate variable. Either solution is
- 3:51:41valid. Use whatever you prefer. This
- 3:51:44problem can also plague loops. For
- 3:51:46example, let's say we wanted to grab an
- 3:51:48array of users from a database.
- 3:51:50Communicating with a database can take
- 3:51:52time. Let's create a function called get
- 3:51:55users.
- 3:51:58From within this function, call the
- 3:52:00sleep function for 2 seconds.
- 3:52:04Next, return an array with random names.
- 3:52:09Now that we have our function, let's try
- 3:52:12creating a for loop. Add this loop below
- 3:52:15the function.
- 3:52:17Let's quickly add the following. I
- 3:52:20equals Z I less than count get users I
- 3:52:24++.
- 3:52:27Lastly, let's echo the I variable.
- 3:52:32Overall, most of this loop is familiar
- 3:52:34to us. I'm introducing a new function
- 3:52:36called count. We don't want the loop to
- 3:52:38run forever. We only want the loop to
- 3:52:40run for as many items as there are in
- 3:52:42the loop. The count function can be used
- 3:52:45for counting the items in an array.
- 3:52:47it'll return the size as an integer.
- 3:52:49That's why we're calling it in this
- 3:52:51example. Similar to before, we're going
- 3:52:53to be waiting 4 seconds for the page to
- 3:52:56render. This is because PHP waits 2
- 3:52:59seconds for the condition to finish.
- 3:53:01It'll execute the block of code and then
- 3:53:03check the condition again. This process
- 3:53:06involves calling the get users function,
- 3:53:08which takes 2 seconds to finish.
- 3:53:12To avoid this scenario, we can outsource
- 3:53:15the value from counting the array into a
- 3:53:17separate variable like so.
- 3:53:23I'm using a variable called user count
- 3:53:26to get this information. This time the
- 3:53:28page takes less time to load. Unlike
- 3:53:31before, the function gets called once
- 3:53:34unlike calling it every time the loop
- 3:53:36checks the condition. Once again,
- 3:53:38storing the results of a function in a
- 3:53:40variable is the best way to go
- 3:53:42sometimes, especially if you're using
- 3:53:44the results in conditions or loops. Just
- 3:53:47something to consider when working on
- 3:53:49your application. In the future, when we
- 3:53:51start working on the master project,
- 3:53:54we'll get plenty of practice optimizing
- 3:53:56our code with this technique. In the
- 3:53:58next lecture, let's continue exploring
- 3:54:00other topics of PHP.
- 3:54:03[Music]
- 3:54:15In this lecture, we're going to start
- 3:54:17adding additional files to our project.
- 3:54:19Realistically, most projects require
- 3:54:22developers to write hundreds of lines of
- 3:54:24code. With so much code, it can be
- 3:54:27difficult to maintain a project when
- 3:54:29everything is written within a single
- 3:54:30file. For this reason, PHP allows us to
- 3:54:34split our codebase into multiple files.
- 3:54:36Let's look at an example. In our
- 3:54:38project, we have two files called
- 3:54:41index.php
- 3:54:43and about.php.
- 3:54:45At the moment, users have to type the
- 3:54:47file name from within the address bar to
- 3:54:49view each page. I don't think that
- 3:54:51provides for a good user experience. The
- 3:54:54most common solution to this problem is
- 3:54:56to create a navigation menu. So, let's
- 3:54:59do that. In the index.php file, clear
- 3:55:03the contents of the file. In the files
- 3:55:05place, add a navigation element.
- 3:55:10Inside the navigation element, add two
- 3:55:13anchor elements linking to the homepage
- 3:55:16and about page.
- 3:55:21Below the navigation, let's add some
- 3:55:24text to identify the homepage.
- 3:55:28In the preview, the navigation appears
- 3:55:31as expected. Everything we've done so
- 3:55:33far is with pure HTML. However, there's
- 3:55:36just one problem. If I were to click on
- 3:55:39the about link, I'm taken to the about
- 3:55:41page. The navigation menu is completely
- 3:55:44missing. This issue can be resolved by
- 3:55:47copying and pasting the navigation over
- 3:55:49to the about file. Seems simple, right?
- 3:55:52Not so fast. What if we need to add,
- 3:55:55remove, or change the links in the
- 3:55:57navigation menu? What if we had more
- 3:55:59than two pages? Sites can have hundreds
- 3:56:02of pages. Copying and pasting the menu
- 3:56:05is not scalable. If we copy the menu to
- 3:56:08each page, we would have to modify every
- 3:56:10file that has the navigation menu.
- 3:56:13Ideally, the navigation menu should be
- 3:56:15easy to modify. Rather than creating
- 3:56:18copies of the navigation menu, let's
- 3:56:20have a single copy in a separate file
- 3:56:22inside our project. Create a file called
- 3:56:25nav.php.
- 3:56:29Next, I'm going to move the navigation
- 3:56:32element from the index file to the
- 3:56:34navigation file.
- 3:56:38The question becomes, how can we load
- 3:56:40the navigation file from both the index
- 3:56:42and about files? PHP has a keyword
- 3:56:45called include which instructs the
- 3:56:48language to load a different file. The
- 3:56:50include keyword accepts a path to a
- 3:56:52file. Let's give it a try. Back in the
- 3:56:55index file, try adding a pair of PHP
- 3:56:58tags. Inside these tags, add the include
- 3:57:01keyword.
- 3:57:04The path to the file must be written as
- 3:57:06a string. Let's set the path to nav.php.
- 3:57:12Just like that, we've loaded the
- 3:57:14navigation menu with a single line of
- 3:57:16code. PHP handles loading the file for
- 3:57:19us. As you can imagine, this feature is
- 3:57:22incredibly helpful for splitting a page
- 3:57:24into separate sections. You can create
- 3:57:27separate files for the header, footer,
- 3:57:29and sidebar. Let's try loading the
- 3:57:32navigation for the about file. In this
- 3:57:34file, I'm going to add the include
- 3:57:36statement. Again, you can copy and paste
- 3:57:39the line of code we had in the previous
- 3:57:41file.
- 3:57:43Great. Now, we can try clicking on the
- 3:57:46links to test our navigation. If we
- 3:57:48switch between pages, we're able to view
- 3:57:50the respective pages content. Best of
- 3:57:53all, we can update the navigation menu
- 3:57:56once and all pages will be reflected
- 3:57:58with those changes. We've just scratched
- 3:58:01the surface. There's more we can do.
- 3:58:03Back in the index file, PHP allows us to
- 3:58:06include files multiple times. For
- 3:58:09example, add the include keyword again
- 3:58:11for the navigation file.
- 3:58:15This time we get two navigation menus.
- 3:58:18That's great and all, but this may be a
- 3:58:20mistake. What if our navigation file had
- 3:58:22PHP code for creating a constant? Let me
- 3:58:25show you an example. In the navigation
- 3:58:28file, add a pair of PHP tags above the
- 3:58:31menu. Inside these tags, create a
- 3:58:33constant called fu with a value of one.
- 3:58:39You can use either the define function
- 3:58:41or const keyword. It doesn't really
- 3:58:44matter. After adding the constant, PHP
- 3:58:47throws an error. The error states that
- 3:58:49the constant has already been created.
- 3:58:51The reason we get this error is because
- 3:58:54we're including the navigation file
- 3:58:56twice. Every time we include a file, PHP
- 3:58:59executes the code inside that file. As a
- 3:59:02result, we're attempting to define the
- 3:59:04same constant twice. We can resolve this
- 3:59:07issue by not including the file twice.
- 3:59:09However, mistakes can happen. If we have
- 3:59:12a large project, it's possible that we
- 3:59:14may accidentally include a file twice.
- 3:59:17To get around this dilemma, we can use
- 3:59:19an alternative keyword. Back in the
- 3:59:21index file, update the include keyword
- 3:59:24to include once.
- 3:59:28After swapping the keywords, the error
- 3:59:30goes away. The include once keyword is
- 3:59:32similar to the include keyword. It'll
- 3:59:34embed the code from another file into
- 3:59:37the current file. The main difference is
- 3:59:39that the code is only executed once. If
- 3:59:42we accidentally include the same file
- 3:59:44twice, PHP will make sure that the code
- 3:59:47only runs once, thus avoiding the error
- 3:59:49encountered earlier. I'm going to remove
- 3:59:52the second line of code. It's not
- 3:59:54necessary to keep it around. In addition
- 3:59:56to the include keyword, there's another
- 3:59:59keyword available for including files
- 4:00:01called require. Let's replace the
- 4:00:03include once keyword with the require
- 4:00:06keyword.
- 4:00:08Similar to before, we get the same
- 4:00:11result. Just like the include keyword,
- 4:00:13the require keyword has an alternative
- 4:00:15variation called require once.
- 4:00:20So you may be wondering what are the
- 4:00:22differences between the include and
- 4:00:24require keywords. Both keywords can be
- 4:00:27used for adding files to the current
- 4:00:29file. The main difference is how they
- 4:00:31handle errors. For example, let's try
- 4:00:34changing the file from nav.php to
- 4:00:37example.php.
- 4:00:41This time an error gets thrown. There's
- 4:00:43a lot of information, but the portion we
- 4:00:46should focus on is the type of error.
- 4:00:48PHP has thrown a fatal error. Whenever
- 4:00:51PHP throws fatal errors, the rest of the
- 4:00:54script does not execute. It doesn't
- 4:00:56matter if we have more code to execute.
- 4:00:59PHP stops running altogether. This is
- 4:01:02proven by the fact that the message
- 4:01:04after the require keyword is missing.
- 4:01:07Let's replace this keyword with the
- 4:01:09include keyword.
- 4:01:12Once again, we get an error. This time
- 4:01:15the homepage message appears after the
- 4:01:17error. Looking closely at the error, PHP
- 4:01:20marks the error as a warning, not a
- 4:01:22fatal error. Unlike fatal errors,
- 4:01:25warnings do not stop a script from
- 4:01:27execution. PHP continues to run the next
- 4:01:30line of code after the warning has been
- 4:01:32thrown.
- 4:01:35Understanding the differences is
- 4:01:37crucial. Not every function or keyword
- 4:01:40produces the same type of error.
- 4:01:42Warnings do not stop a script from
- 4:01:44execution. PHP simply outputs a message
- 4:01:47and then proceeds to the next line of
- 4:01:49code. Whereas fatal errors completely
- 4:01:52stop the script from running after the
- 4:01:55errors have been rendered on the page.
- 4:01:59So which keyword should you use? Well,
- 4:02:02that depends on if the file is
- 4:02:03necessary. If the file is necessary, use
- 4:02:06the require keyword. However, if your
- 4:02:09application can function fine without
- 4:02:11the file, use the include keyword.
- 4:02:14Throughout the course, you'll see plenty
- 4:02:16of examples. There's one more thing I
- 4:02:19want to show you before moving on.
- 4:02:21Believe it or not, files that are
- 4:02:23included can return data. For example,
- 4:02:26let's create a new file in our project
- 4:02:28called example.php.
- 4:02:33Inside this file, add an opening php
- 4:02:36tag. We're going to use the return
- 4:02:38keyword to return the number two.
- 4:02:43The return keyword is not exclusive to
- 4:02:46functions. We can add it outside of a
- 4:02:48function to return data from the overall
- 4:02:50file. Back on the index.php file, we can
- 4:02:54store the data returned by our file in a
- 4:02:57variable like we would with any other
- 4:02:59type of value. Let's store the result in
- 4:03:02a variable called data.
- 4:03:06Lastly, let's echo the variable
- 4:03:10in the preview. The number appears.
- 4:03:13That's great. This feature can be handy
- 4:03:15whenever you outsource the data in a
- 4:03:17separate file instead of just
- 4:03:19outsourcing HTML. Keep in mind the same
- 4:03:22behavior applies to files as it does
- 4:03:25functions. If we return data, the rest
- 4:03:28of the file does not execute. For
- 4:03:30example, let's head back to the example
- 4:03:33file. Before returning a number, let's
- 4:03:36return one.
- 4:03:39After the return statement, echo the
- 4:03:41number three.
- 4:03:44In the preview, the numbers one are
- 4:03:46rendered on the screen. However, the
- 4:03:48number three is completely missing.
- 4:03:51That's expected since the value is
- 4:03:53echoed after the return statements. PHP
- 4:03:56does not bother executing the rest of
- 4:03:58the file after a value is returned.
- 4:04:01Remember this behavior only applies to
- 4:04:03the file that was included. The file
- 4:04:06that performs the inclusion can continue
- 4:04:09executing more code. That's about it for
- 4:04:11including files with PHP. In the next
- 4:04:14lecture, let's start diving into
- 4:04:16features about functions.
- 4:04:18[Music]
- 4:04:30In this lecture, we're going to make our
- 4:04:33functions more flexible by using the
- 4:04:35spread operator in our functions. To
- 4:04:38understand why this operator can be
- 4:04:40useful, let's look at a scenario where
- 4:04:42it can be useful in our script. Let's
- 4:04:44replace the current code with a function
- 4:04:47called sum.
- 4:04:50The goal of this function will be to
- 4:04:52calculate the sum of two numbers. Let's
- 4:04:55add two parameters called a and b. Their
- 4:04:58data types will be a union type of the
- 4:05:00integer and float types.
- 4:05:04Next, inside the function return a + b.
- 4:05:10Lastly, let's use our new function by
- 4:05:13echoing the function with the numbers
- 4:05:15five and two.
- 4:05:18Our function is working as expected.
- 4:05:20It's possible to improve upon the
- 4:05:22existing functionality. Sometimes we may
- 4:05:25want to calculate the sum of three
- 4:05:26numbers or seven numbers. At the moment,
- 4:05:29our function is limited to two numbers.
- 4:05:31One solution would be to add more
- 4:05:33parameters. That would work, but it
- 4:05:35would be tedious. It would be ridiculous
- 4:05:37to have 20 parameters. An alternative
- 4:05:40solution would be to create a variatic
- 4:05:43function. A variatic function is a
- 4:05:45function that accepts an unlimited
- 4:05:47number of parameters. It's easy to
- 4:05:49create. In the parameter list, replace
- 4:05:52the parameters with a parameter called
- 4:05:54numbers.
- 4:05:57Next, before the parameters name, we can
- 4:06:00add three dots, also known as the spread
- 4:06:03operator. Actually, there isn't an
- 4:06:05official name for this operator. Some
- 4:06:07developers refer to it as the splat
- 4:06:09operator. Either way, adding this
- 4:06:12operator to a parameter instructs PHP to
- 4:06:15accept unlimited arguments. Arguments
- 4:06:18passed into the function are stored as
- 4:06:20an array which we can access via the
- 4:06:22parameter. Time hinting is still
- 4:06:24supported before the arguments add the
- 4:06:26integer and float data types.
- 4:06:31Just to verify that the values are an
- 4:06:33array of integers, let's call the
- 4:06:35variable dump function with the numbers
- 4:06:37parameters.
- 4:06:40We do get a warning, but let's ignore
- 4:06:42that for the time being. At the top of
- 4:06:44the page, the variable dump function
- 4:06:47outputs an array with the numbers passed
- 4:06:49in. By using the spread operator, we've
- 4:06:52created a variatic function. It can
- 4:06:54accept multiple numbers. The next step
- 4:06:57is to loop through the array to add the
- 4:06:59numbers together. Fortunately, PHP has a
- 4:07:02function for performing this exact
- 4:07:04operator. Let's return the following.
- 4:07:07Array sum numbers.
- 4:07:11The array sum function loops through an
- 4:07:14array of numbers and calculates the sum.
- 4:07:16We don't have to create a custom loop to
- 4:07:19achieve the same behavior. After
- 4:07:21returning the value, the preview
- 4:07:23displays the same result as before. This
- 4:07:25time we can pass in additional numbers
- 4:07:28to the function.
- 4:07:31Our function will be able to handle any
- 4:07:33number of arguments. There is one thing
- 4:07:36to keep in mind when using variatic
- 4:07:38functions. The variatic argument must
- 4:07:41always be the last parameter. For
- 4:07:43example, let's say we wanted to add the
- 4:07:45option to dump the array before
- 4:07:47returning the value. Add a boolean
- 4:07:50parameter called dump array.
- 4:07:54At the top of the function, add a
- 4:07:56conditional statement with the dump
- 4:07:58array parameter as the condition.
- 4:08:02Next, inside the conditional block of
- 4:08:05code, call the variable dump function
- 4:08:07with the numbers parameter.
- 4:08:11Lastly, let's pass in false as the first
- 4:08:14argument to the sum function.
- 4:08:18So far, everything is working as
- 4:08:20intended. We can pass in true or false
- 4:08:23to affect the behavior of the function.
- 4:08:26However, what if we switch the
- 4:08:28parameters? Let's find out.
- 4:08:32After doing so, PHP throws a fatal
- 4:08:35error. The message is cut and clear.
- 4:08:37It's informing us that our variatic
- 4:08:39parameter must be last, which makes
- 4:08:42sense, right? After all, the dump array
- 4:08:44parameter can never be assigned a value
- 4:08:47if the numbers array takes in all the
- 4:08:49values. So, keep this in mind when using
- 4:08:52variatic functions. In the next lecture,
- 4:08:55let's keep exploring functions.
- 4:08:57[Music]
- 4:09:08In this lecture, we're going to add
- 4:09:10named arguments to our function. Named
- 4:09:13arguments are a new feature of PHP8. If
- 4:09:16you're using an older version of PHP,
- 4:09:19this feature won't work. Named arguments
- 4:09:21are a feature for passing a value to a
- 4:09:24specific parameter. Let's look at an
- 4:09:26example by simplifying our function.
- 4:09:28First, we should update our function to
- 4:09:31accept three parameters. Instead of
- 4:09:33creating a variatic function, let's
- 4:09:35revert the function to its original
- 4:09:37solution, which was to accept two
- 4:09:40numbers called A and B.
- 4:09:44The dump array parameter can be removed
- 4:09:47along with the conditional statement.
- 4:09:51Afterward, update the return statement
- 4:09:54to add the A and B parameters.
- 4:09:58Lastly, let's update our echo statement
- 4:10:01by passing in five and two.
- 4:10:05Awesome. We've got something similar to
- 4:10:07what we had before. So, how do named
- 4:10:10arguments work? Currently, PHP assigns
- 4:10:13the values to our parameters based on
- 4:10:15the order of the values passed in. The
- 4:10:18number five is assigned to the A
- 4:10:20parameter and the number two is assigned
- 4:10:23to the B parameter. What if we want to
- 4:10:25reverse those assignments? Well, that's
- 4:10:28possible with named arguments. Before
- 4:10:30the values, we can add the name of the
- 4:10:33parameters like so. B colon and A colon.
- 4:10:40By writing the name of the parameters
- 4:10:42before the values, PHP handles assigning
- 4:10:45each value to the correct parameter
- 4:10:47regardless of the order. We can prove
- 4:10:50this further by dumping both parameters
- 4:10:52from within the function. As you can
- 4:10:54see, the B parameter holds the number
- 4:10:57two and the A parameter holds the number
- 4:11:00five. In our example, using named
- 4:11:02arguments is redundant. The result is
- 4:11:05going to be the same. So why would you
- 4:11:07want to use named arguments? In the
- 4:11:10resource section of this lecture, I
- 4:11:12provide a link to a function called set
- 4:11:15cookies. PHP has a few functions with
- 4:11:18more than five parameters. Sometimes you
- 4:11:21may have custom functions with a lot of
- 4:11:23parameters. Looking closely at the
- 4:11:26definition, some parameters are
- 4:11:28optional. Let's imagine that we wanted
- 4:11:30to call this function but we only needed
- 4:11:32to set the name and HTTP only
- 4:11:35parameters. If we want to set the HTTPON
- 4:11:39parameter, we must provide values for
- 4:11:41the other parameters, even if it's their
- 4:11:44default values by using named arguments.
- 4:11:47We can skip setting values for the other
- 4:11:50parameters and only focus on what we
- 4:11:52want to set. Named arguments improve
- 4:11:55readability in our code by allowing us
- 4:11:57to configure specific parameters and
- 4:12:00forget the rest. So that's how named
- 4:12:03arguments work. We'll be using them from
- 4:12:05time to time in our projects.
- 4:12:07[Music]
- 4:12:19In this lecture, we're going to talk
- 4:12:21about global variables. Before doing so,
- 4:12:24we should talk about the concept of
- 4:12:26scope. Scope affects the way we write
- 4:12:28programs. So, what is scope? Scope
- 4:12:31refers to the accessibility of your
- 4:12:33variables based on their location. The
- 4:12:36location of where your variables are
- 4:12:38defined matters a lot. If you define a
- 4:12:40variable from within a function, it'll
- 4:12:42be inaccessible outside of the function.
- 4:12:45Vice versa, variables defined outside of
- 4:12:48a function are inaccessible from within
- 4:12:50a function unless we use a keyword
- 4:12:53called global. Let's look at an example
- 4:12:56in our script. Let's replace the current
- 4:12:59example with a variable called x with a
- 4:13:02value of five.
- 4:13:06By defining the variable outside of a
- 4:13:08function, it's accessible everywhere.
- 4:13:10This includes other files. For example,
- 4:13:13let's open the example.php
- 4:13:16file.
- 4:13:19Inside this file, let's increment the x
- 4:13:21variable with the increment operator.
- 4:13:26Next, let's echo the variable.
- 4:13:31Technically, this variable doesn't exist
- 4:13:33within the file. Therefore, we'll get an
- 4:13:36error if we visit the file directly.
- 4:13:39However, it can become available if we
- 4:13:41include this file from the index file.
- 4:13:44Back in the index file, add the include
- 4:13:46keyword to load the example.php
- 4:13:49file.
- 4:13:52Be sure to add this statement after the
- 4:13:54variable has been created. If we look in
- 4:13:57the preview, the number six has been
- 4:13:59outputed. Despite the variable being
- 4:14:02defined in another file, PHP exposes our
- 4:14:05variables to other files, they just have
- 4:14:08to be defined before they're accessed.
- 4:14:10This is why we must include the file
- 4:14:13after declaring the variable. Let's take
- 4:14:15it a step further by using the function
- 4:14:18from within a function. After the
- 4:14:20include statement, define a function
- 4:14:22called fu.
- 4:14:25Inside this function, let's echo the x
- 4:14:27variable.
- 4:14:30Lastly, let's call the foo function.
- 4:14:35After doing so, PHP throws an error at
- 4:14:37us. It's saying that the X variable is
- 4:14:40undefined, which is strange. However,
- 4:14:42it's not strange once you consider
- 4:14:44scope. Functions have a local scope.
- 4:14:47They don't acknowledge variables defined
- 4:14:49from outside the function. This is why
- 4:14:51we're receiving an error. There is a
- 4:14:53solution to override this behavior. At
- 4:14:56the top of the function, we could
- 4:14:57include the global keyword.
- 4:15:01This keyword instructs PHP to look in
- 4:15:04the global scope, which is where
- 4:15:06variables exist when defined outside
- 4:15:08functions. We can provide a list of
- 4:15:10variables to grab from the global scope
- 4:15:13by typing their name after this keyword.
- 4:15:15Let's grab the X variable. If we had
- 4:15:18multiple variables, we would separate
- 4:15:20them with commas. After adding this
- 4:15:22variable to the list, the error goes
- 4:15:25away. One thing to keep in mind is that
- 4:15:27the function can be modified. If we
- 4:15:29modify it from within the function,
- 4:15:31it'll affect other uses of the variable.
- 4:15:34For example, let's use the increment
- 4:15:36operator on the variable after echoing
- 4:15:38the value.
- 4:15:42Next, let's echo the variable one more
- 4:15:44time. After calling the foo function
- 4:15:48in the output, the number six gets
- 4:15:51outputed twice because we're echoing the
- 4:15:53value from within the example file and
- 4:15:55foo function. Next, the numbers get
- 4:15:58outputed one more time, but it's been
- 4:16:00changed to seven because the value was
- 4:16:02incremented from within the foo
- 4:16:04function. A lot of developers don't like
- 4:16:06global variables for this reason.
- 4:16:09Variables that are accessible to every
- 4:16:11file and function means that the value
- 4:16:13can change at any time. Therefore,
- 4:16:15variables become less reliable. But what
- 4:16:18if we need to update a variable? The
- 4:16:21recommended solution is to use
- 4:16:23parameters and return values by passing
- 4:16:26in the variable as an argument to the
- 4:16:28function. The function will be able to
- 4:16:30modify the value and then return the
- 4:16:32result. The result can then be stored in
- 4:16:35the variable. Throughout the course, I'm
- 4:16:37going to be using parameters and return
- 4:16:39values to update variables. Global
- 4:16:42variables are not something I'm fond of
- 4:16:44either. However, you may come across
- 4:16:46global variables from time to time on
- 4:16:49existing projects. They were popular
- 4:16:51once upon a time ago. It's good to know
- 4:16:53how they work. In the next lecture,
- 4:16:55let's keep talking about variables.
- 4:16:58[Music]
- 4:17:10In this lecture, we're going to discuss
- 4:17:12static variables. One thing to
- 4:17:14understand about functions is that
- 4:17:16variables are deleted after the function
- 4:17:18finishes running. Let's look at an
- 4:17:20example in our script. Replace the
- 4:17:23current example with a function called
- 4:17:25fu.
- 4:17:28Inside this function, let's create a
- 4:17:30variable called a with an initial value
- 4:17:32of one.
- 4:17:35Lastly, let's return the variable with
- 4:17:38the increment operator.
- 4:17:41After defining the function, let's call
- 4:17:44it three times with a break element.
- 4:17:50In the preview, the number appears three
- 4:17:52times. However, it's the same number in
- 4:17:55our function. The variable's initial
- 4:17:57value is one. After the value gets
- 4:17:59returned, the number should update by
- 4:18:02one, resulting in the variable changing
- 4:18:04its value to two. Here's the thing. PHP
- 4:18:07destroys a variable after the function
- 4:18:10is finished running. As a result,
- 4:18:12calling the function again resets the
- 4:18:14variable. This is why we're getting the
- 4:18:16same output. This behavior is
- 4:18:18beneficial. Most of the time, we don't
- 4:18:20need the variable to see around after
- 4:18:22the function is finished running. It's
- 4:18:24nice that PHP performs some cleanup for
- 4:18:27us. In some cases, you may want to keep
- 4:18:30the variable around after the function
- 4:18:32is finished running. This feature is
- 4:18:34especially useful when you're writing
- 4:18:36code that requires a lot of resources.
- 4:18:39Creating variables can easily become an
- 4:18:41expensive operation. The more data you
- 4:18:44have, the longer it can take to run a
- 4:18:46script. To avoid wasting resources, we
- 4:18:49can use static variables. Static
- 4:18:52variables are variables inside functions
- 4:18:54that retain their value after the
- 4:18:56function is finished running. We can
- 4:18:58create a static variable by adding the
- 4:19:01static keyword before the variable name
- 4:19:03like so.
- 4:19:06After adding this keyword, the output
- 4:19:09changes. Instead of the same number, the
- 4:19:11number slowly increments to three. Using
- 4:19:14the static keyword is incredibly
- 4:19:16powerful for keeping our programs
- 4:19:18efficient. I'm sure you'll find it
- 4:19:20useful. In the next lecture, let's get
- 4:19:22into a discussion on anonymous
- 4:19:24functions.
- 4:19:26[Music]
- 4:19:38In this lecture, we're going to explore
- 4:19:40a strange topic that may be hard to wrap
- 4:19:42your head around. I'll try my best to
- 4:19:45explain it. So, what am I talking about?
- 4:19:47I'm referring to a feature called
- 4:19:49anonymous functions. An anonymous
- 4:19:52function is just a function that doesn't
- 4:19:54have a name. As always, let's look at an
- 4:19:56example. In our script, replace the
- 4:19:59contents with a function called
- 4:20:01multiply.
- 4:20:04This function will accept a parameter
- 4:20:07called num.
- 4:20:10Inside the function, let's return the
- 4:20:12num parameter multiplied by two.
- 4:20:17Lastly, let's invoke the function with a
- 4:20:19random number.
- 4:20:22The function is pretty straightforward.
- 4:20:24We're multiplying a number by two.
- 4:20:26Nothing fancy so far. Believe it or not,
- 4:20:29PHP allows us to store a function from
- 4:20:32within a variable. Doing so is very
- 4:20:34simple. First, we must convert the
- 4:20:36function into an anonymous function by
- 4:20:39removing the name.
- 4:20:42After removing the name, the preview
- 4:20:44generates an error. The reason is
- 4:20:46simple. Anonymous functions are
- 4:20:48considered to be expressions. Like most
- 4:20:51expressions, we must end the line of
- 4:20:53code with a semicolon character.
- 4:20:55Typically, this character can be omitted
- 4:20:58when using curly brackets for blocks of
- 4:21:00code. However, this is the exception to
- 4:21:02the rule. Since we have an expression,
- 4:21:05it's perfectly acceptable to store the
- 4:21:07function from within a variable. Let's
- 4:21:09set the function to a variable called
- 4:21:12multiply.
- 4:21:15We can invoke the function like any
- 4:21:17other function. However, we must include
- 4:21:19the dollar sign character at the
- 4:21:21beginning of the function name. After
- 4:21:23applying those changes, our code works
- 4:21:26like before. We're able to reference a
- 4:21:28function from a variable. But why would
- 4:21:31we want to store a function in a
- 4:21:32variable to begin with? There are two
- 4:21:35advantages to doing so. Firstly, the
- 4:21:37function can be swappable. Previously,
- 4:21:40we weren't allowed to define a function
- 4:21:42twice. If we had two functions with the
- 4:21:45same name, PHP would throw an error.
- 4:21:48However, since variables can have their
- 4:21:50values updated, we can easily swap this
- 4:21:53function with another function. For
- 4:21:55example, after defining the multiply
- 4:21:58variable, let's try updating its value
- 4:22:00to another anonymous function. It's
- 4:22:03going to have the same parameters and
- 4:22:05return value. However, we're going to
- 4:22:08multiply the num parameter by three
- 4:22:10instead of two.
- 4:22:14In the preview, the page outputs a
- 4:22:17different value than before. This is
- 4:22:19because it's using our new anonymous
- 4:22:21function instead of the previous
- 4:22:23function. Replacing functions is a
- 4:22:25powerful feature. It allows us to alter
- 4:22:28the behavior of our application at a
- 4:22:30moment's notice. The second reason this
- 4:22:32feature is useful is because it implies
- 4:22:35that a function can be passed onto
- 4:22:37another function as an argument. Let's
- 4:22:40look at an example. First, I'm going to
- 4:22:42remove the second anonymous function. It
- 4:22:45isn't necessary anymore.
- 4:22:49Next, define a function called sum. It's
- 4:22:51a function we've defined before. It'll
- 4:22:54accept two parameters called a and b.
- 4:22:57Lastly, we'll return the result by
- 4:23:00adding them together.
- 4:23:03It's a pretty standard function. We can
- 4:23:05expand on this function by allowing
- 4:23:07developers to pass in an additional
- 4:23:09function to modify the result.
- 4:23:13In the parameter list, add a parameter
- 4:23:16called callback.
- 4:23:19Before going any further, let's talk
- 4:23:22about the idea of a call back.
- 4:23:26The idea of a call back is not specific
- 4:23:28to PHP. It can be applied to various
- 4:23:31programming languages. A call back is a
- 4:23:33function passed into another function
- 4:23:35that can be called later. It's like if
- 4:23:38you gave your phone number to someone so
- 4:23:39that they can call you at a later time.
- 4:23:42It's the same idea, but with functions.
- 4:23:45You're passing in a function that will
- 4:23:46be called at a later time. Hence why
- 4:23:49it's called a callback function.
- 4:23:53If you ever define a function that
- 4:23:55accepts a callback, it's standard
- 4:23:57practice to name the parameter call
- 4:23:59back. A lot of developers adopt this
- 4:24:02practice. We'll be following it too.
- 4:24:04After adding this parameter, let's wrap
- 4:24:06our calculation with this function.
- 4:24:11Next, let's update our echo statement to
- 4:24:14pass in another function. It just so
- 4:24:16happens that we have a function called
- 4:24:18multiply that we can use.
- 4:24:23Now, if we look at the page, a new
- 4:24:25number gets produced. We're adding the
- 4:24:27numbers passed into the function and
- 4:24:29multiplying the result by two. Best of
- 4:24:32all, we're able to swap callback
- 4:24:34functions with different functions. One
- 4:24:36moment we may want to multiply the
- 4:24:38result, another moment we may want to
- 4:24:40divide the results. Since we're not tied
- 4:24:43to a specific function, we can easily
- 4:24:45change the result without directly
- 4:24:47modifying the original function. Using
- 4:24:50anonymous functions gives us this power.
- 4:24:53There's another advantage to using
- 4:24:54anonymous functions. They can access
- 4:24:57global variables without directly
- 4:24:59modifying the original value. Let me
- 4:25:02show you what I mean. Above the multiply
- 4:25:04function, define a variable called
- 4:25:06multiplier with an initial value of two.
- 4:25:12Next, let's replace the number that gets
- 4:25:14multiplied against the number parameter
- 4:25:17with the multiplier variable.
- 4:25:21Now, we have a variable for changing the
- 4:25:24number that gets multiplied against the
- 4:25:26parameter. This makes things more
- 4:25:28flexible. However, as we know, variables
- 4:25:31defined outside of the function can't be
- 4:25:34used. One solution would be to use the
- 4:25:36global keyword. But there's an even
- 4:25:38better solution. After the parameter
- 4:25:40list, add the use keyword.
- 4:25:44The use keyword provides access to
- 4:25:47variables outside of the function. We
- 4:25:49can add a pair of parentheses followed
- 4:25:51by a list of variables to grab separated
- 4:25:54by commas. Let's include the multiplier
- 4:25:56variable.
- 4:25:59You may be wondering, how is this any
- 4:26:01better? Here's the interesting thing.
- 4:26:03PHP creates a copy of the variable
- 4:26:06instead of directly giving us access to
- 4:26:08the original variable. For example, in
- 4:26:11the anonymous function, let's change the
- 4:26:13multiplier variable to five.
- 4:26:17If we look at the preview, the code is
- 4:26:20working. Unlike before, the number is
- 4:26:22even higher. This is because we're
- 4:26:24modifying the multiplier variable to a
- 4:26:27larger value. However, the multiplier
- 4:26:29variable is just a copy of the original.
- 4:26:32At the end of our code, let's add a line
- 4:26:35break element and echo the multiplier
- 4:26:37variable.
- 4:26:40As you can see, the multiplier variable
- 4:26:43retains its original value. We don't
- 4:26:45suffer from the problems of a global
- 4:26:47variable where the value does get
- 4:26:49modified after the function finishes
- 4:26:52running. It's much safer to use
- 4:26:54anonymous functions. There's one last
- 4:26:56thing I want to show you before moving
- 4:26:58on. In PHP 7.4, Four arrow functions
- 4:27:02were introduced. They're a shorter way
- 4:27:04of writing anonymous functions. Let's
- 4:27:06convert our anonymous function into an
- 4:27:08arrow function. It's very easy to do.
- 4:27:11First, change the keyword from function
- 4:27:14to fn.
- 4:27:17Next, replace the use keyword with a fat
- 4:27:20arrow.
- 4:27:23Fat arrows are written with equals
- 4:27:25greater than characters. Afterward, we
- 4:27:28can remove the curly brackets and return
- 4:27:30keyword.
- 4:27:33That leaves us with the expression.
- 4:27:35Arrow functions are just one line of
- 4:27:37code to the right of the arrow. The code
- 4:27:40must be an expression. This is because
- 4:27:42arrow functions must always return a
- 4:27:44value. We don't have to include the
- 4:27:46return keyword because PHP already
- 4:27:49returns the value. In addition, the use
- 4:27:52keyword does not need to be included.
- 4:27:54Unlike regular or the other version of
- 4:27:57anonymous functions, arrow functions
- 4:27:59have access to variables from the parent
- 4:28:01scope. If we look at the preview, we get
- 4:28:04the same result as before. Arrow
- 4:28:06functions are shorter and easier to
- 4:28:08write, but there are some caveats.
- 4:28:10Firstly, we must always return a value.
- 4:28:13Anonymous functions don't have to return
- 4:28:15a value, but arrow functions do.
- 4:28:18Secondly, arrow functions can only
- 4:28:20contain an expression. We can't add
- 4:28:22curly brackets to add additional logic.
- 4:28:25We're restricted to a single line of
- 4:28:26code. As long as you keep these points
- 4:28:29in mind, I think you'll be fine. In the
- 4:28:31next lecture, let's keep talking about
- 4:28:33functions.
- 4:28:35[Music]
- 4:28:46In this lecture, let's talk about the
- 4:28:48callable type, which is a type we
- 4:28:50avoided during our discussion of data
- 4:28:52types. The callable data type is a data
- 4:28:55type you can use for callback functions.
- 4:28:57Let's keep working on our example from
- 4:29:00the previous lecture. We have a function
- 4:29:02called sum with three parameters. The
- 4:29:04first two parameters can be annotated
- 4:29:06with the integer and float types as a
- 4:29:09union type.
- 4:29:12But what about the callback parameter?
- 4:29:15In this case, we can use the callable
- 4:29:17type.
- 4:29:19By using this type, we're telling PHP
- 4:29:22that this parameter will store a
- 4:29:24function. Looking at the preview,
- 4:29:26everything is working as it did before.
- 4:29:28There are different ways of passing in a
- 4:29:30function. The first approach is to store
- 4:29:32the function in a variable and then pass
- 4:29:34it in like we're doing currently.
- 4:29:36Another way of doing it is by just
- 4:29:38passing the function directly from the
- 4:29:40function invocation. For example, let's
- 4:29:43copy and paste the value of the multiply
- 4:29:46function into the sum function like so.
- 4:29:51This time we're directly passing on the
- 4:29:53function. It's common practice to write
- 4:29:55the function directly from within the
- 4:29:57function call. If you don't plan on
- 4:29:59using the function elsewhere, it can be
- 4:30:01convenient to just write it here. In the
- 4:30:04preview, our code is still working. The
- 4:30:06last approach would be to just pass in a
- 4:30:09regular function. Regular functions are
- 4:30:11accepted, too. Below the multiply
- 4:30:13variable, let's define a regular
- 4:30:15function called another multiply. It's
- 4:30:18going to have the exact same logic as
- 4:30:20the first multiply function.
- 4:30:25In this example, I'm not using the
- 4:30:27multiplier variable for the sake of
- 4:30:29simplicity. Next, in the echo
- 4:30:31statements, we can replace the arrow
- 4:30:33function with a string that contains the
- 4:30:35name of the another multiply function.
- 4:30:40If we plan on using a regular function,
- 4:30:42we can pass in the name as a string. PHP
- 4:30:45handles finding and calling the correct
- 4:30:48function. In the preview, everything is
- 4:30:50working as it did before. Overall, the
- 4:30:53callable type is the correct type for
- 4:30:55parameters that store functions. In the
- 4:30:57next lecture, let's get into a
- 4:30:59discussion on references and values.
- 4:31:02[Music]
- 4:31:14In this lecture, we're going to learn
- 4:31:16how to pass on a value by reference.
- 4:31:18This concept tends to be confusing for
- 4:31:21beginners. It can be difficult to wrap
- 4:31:23your head around. Before we get into a
- 4:31:25code example, let's look at a common
- 4:31:27analogy that I think perfectly explains
- 4:31:30passing by references. First, let's talk
- 4:31:33about how a regular parameter works.
- 4:31:35Throughout this course, we've defined a
- 4:31:38few functions with parameters. Every
- 4:31:40time we pass a value to the function,
- 4:31:42PHP creates a copy of the value that
- 4:31:45gets stored in the parameter. If we
- 4:31:47modify the parameter, only the
- 4:31:49parameter's copy of the value gets
- 4:31:51modified. The original value remains
- 4:31:54unaffected. In this example, let's
- 4:31:56imagine we had a variable called cup
- 4:31:59defined outside of a function. Next, we
- 4:32:02may have a function called fill cup. If
- 4:32:04we pass the cup variable to this
- 4:32:06function, the fill cup function is going
- 4:32:09to store a copy of the cup. If the
- 4:32:11function fills the cup with coffee, only
- 4:32:14the copy gets affected. the original cup
- 4:32:16variable remains empty. This is the
- 4:32:19default behavior for parameters. We
- 4:32:21refer to this behavior as pass by value.
- 4:32:25On the other hand, pass by reference
- 4:32:27works differently. Passing a variable by
- 4:32:30reference changes the default behavior.
- 4:32:32Instead of creating a copy of a
- 4:32:34variable, we have direct access to the
- 4:32:36original variable. In this example, the
- 4:32:39cup variable gets passed into the fill
- 4:32:42cup function. If the fill cup function
- 4:32:44affects the cup variable, the original
- 4:32:47copy gets affected. Hopefully, this
- 4:32:50gives you a better idea of how passing
- 4:32:52by reference and passing by value work.
- 4:32:55Let's start looking at a code example
- 4:32:59in our script. Let's replace the current
- 4:33:01code with a fresh example. Actually,
- 4:33:04let's try recreating the example from
- 4:33:06the image I just showed you. Define a
- 4:33:08variable called cup. Its initial value
- 4:33:11will be a string that says empty.
- 4:33:16Next, define a function called fill cup
- 4:33:18with a parameter called cup parameter.
- 4:33:23Inside the function, let's update the
- 4:33:26cup parameter variable to filled.
- 4:33:31After defining the function, call the
- 4:33:33fill cup function with the cup variable.
- 4:33:38Lastly, let's echo the cup variable.
- 4:33:43As expected, the cup remains empty. This
- 4:33:46is because the cup parameter variable is
- 4:33:49a unique copy of our cup variable.
- 4:33:51Modifying this parameter only affects
- 4:33:54the variable inside the function. The
- 4:33:56original cup variable retains its
- 4:33:58original value. What if we don't want to
- 4:34:01create a unique copy? We may want to
- 4:34:03continue using the original copy. In
- 4:34:06this case, we can add the amperand
- 4:34:08character before the parameter name like
- 4:34:11so.
- 4:34:14This time, if we look at the preview,
- 4:34:16the cup has been filled. PHP is no
- 4:34:18longer creating a unique copy of the
- 4:34:21variable. The function can directly
- 4:34:23modify the original variable. So, you
- 4:34:26may be wondering, how is this any better
- 4:34:28than global variables? Isn't modifying
- 4:34:31variables outside of a function bad? One
- 4:34:34difference between global variables and
- 4:34:37variable references is the flexibility
- 4:34:39offered by references. Let's say that I
- 4:34:42was out dining with friends. Each of my
- 4:34:44friends can have a cup. So, I'll change
- 4:34:47the variable name to my cup.
- 4:34:51Next, I'll create another variable
- 4:34:54called my friend's cup with the same
- 4:34:56value.
- 4:34:59Now, we have two cups in the function.
- 4:35:02If we were to use global variables, I
- 4:35:04would have to list out every cup that I
- 4:35:06want to modify.
- 4:35:10As you can imagine, this is not
- 4:35:11scalable. What if we had 20 cups?
- 4:35:14Listing each variable is not efficient.
- 4:35:17Instead of using global variables,
- 4:35:19references are much easier to work with.
- 4:35:22We can call the function for each
- 4:35:24variable. Even better, we don't have to
- 4:35:26call the function for each cup. Some
- 4:35:29cups can just remain unfilled.
- 4:35:31Alternatively, we may have another
- 4:35:33function for filling the cup with juice
- 4:35:35instead of coffee. Overall, passing by
- 4:35:38reference gives us the flexibility to
- 4:35:40pass in any variable instead of passing
- 4:35:43in a specific variable. Hopefully, that
- 4:35:46makes sense. In the next lecture, let's
- 4:35:48start using our newfound knowledge of
- 4:35:51functions to talk about arrays.
- 4:35:53[Music]
- 4:36:05In this lecture, let's explore the
- 4:36:07various functions for arrays. Arrays are
- 4:36:10extremely popular in PHP development. If
- 4:36:13you think about it, almost everything is
- 4:36:15a collection of data. We can have a list
- 4:36:18of posts, products, users, transactions,
- 4:36:21etc. So many of the sites we visit
- 4:36:24present at least one list. You're very
- 4:36:26likely to work with arrays on almost
- 4:36:28every page you create. In the resource
- 4:36:31section of this lecture, I provide a
- 4:36:33link to an official list of functions
- 4:36:35for arrays. Arrays are so popular, PHP
- 4:36:39offers dozens of functions for working
- 4:36:41with them. Almost any type of action
- 4:36:43you'd like to perform on an array can be
- 4:36:46performed with PHP's functions. I said
- 4:36:49this before, but it bears repeating. You
- 4:36:51won't be able to memorize every function
- 4:36:53offered by PHP. However, there are some
- 4:36:57functions that you'll likely use more
- 4:36:59than others. This lecture covers some of
- 4:37:01the most common functions for working
- 4:37:03with arrays. Let's get started. Switch
- 4:37:06over to replet. Replace the current
- 4:37:08codebase with a variable called users.
- 4:37:12This variable will be an array of names.
- 4:37:14I'll add the following names: John,
- 4:37:17Jane, and Bob.
- 4:37:20One of the most common scenarios is to
- 4:37:23check for an existing item in an array.
- 4:37:25As we know, PHP throws errors if we
- 4:37:28attempt to access an item in an array
- 4:37:31that doesn't exist. To avoid this issue,
- 4:37:34it's common practice to check that an
- 4:37:36item exists before doing anything else.
- 4:37:39PHP offers two functions for this
- 4:37:41scenario. The most common function to
- 4:37:44use is called is set. Below the
- 4:37:46variable, add a conditional statement
- 4:37:48with the is set function.
- 4:37:53This function checks that a variable has
- 4:37:55a value. It's not exclusive to arrays.
- 4:37:58We can use it to check for strings,
- 4:38:00integers, floats, and booleans. Let's
- 4:38:03check if the first item in the users
- 4:38:05array has a value by passing in users
- 4:38:08zero.
- 4:38:11Inside the conditional block, let's echo
- 4:38:14a message that says user found.
- 4:38:18In the preview, the user was found. The
- 4:38:21is set function returns a boolean value
- 4:38:24based on whether the user could be
- 4:38:26found. Everything works, but there is
- 4:38:28one thing we need to be aware of. What
- 4:38:31if we change the index from 0 to three?
- 4:38:36As expected, nothing appears because
- 4:38:38there isn't an item in the third index.
- 4:38:41Let's try adding null to the array.
- 4:38:45Despite adding a new item to the array,
- 4:38:48a user hasn't been found. Technically,
- 4:38:51there's an item in the third index.
- 4:38:53However, the is set function considers
- 4:38:56null and false values to be the same
- 4:38:59thing as not having a value in that
- 4:39:01index. You may disagree with that
- 4:39:03behavior. For this reason, PHP
- 4:39:06introduces a completely different
- 4:39:08function for performing a similar task
- 4:39:10as the is set function called array key
- 4:39:14exists. Unlike the is set function, this
- 4:39:17function is designed for arrays only.
- 4:39:19Let's replace the condition with this
- 4:39:21function.
- 4:39:24This function has two parameters. The
- 4:39:27first parameter is the index which can
- 4:39:29be a string or integer. Let's pass in
- 4:39:32three. Next, we must pass in the array
- 4:39:34itself. Let's pass in the user's array.
- 4:39:39This time, the user has been found. The
- 4:39:42array key exists function allows for
- 4:39:45null or false values. As long as there's
- 4:39:48a value in a specific index, the
- 4:39:50function returns true. Let's move on to
- 4:39:53another function. In our array, we have
- 4:39:55a null value. It's not uncommon to have
- 4:39:58values in your array that you don't want
- 4:40:00to keep around. PHP has a function
- 4:40:03called array filter for filtering items
- 4:40:06out of an array. Let's replace the
- 4:40:08conditional statement with a
- 4:40:10reassignment of the users variable. Set
- 4:40:12the variable to the array filter
- 4:40:15function.
- 4:40:18This function accepts the array to
- 4:40:20filter. Let's pass in the user's array.
- 4:40:25Lastly, let's print the contents of the
- 4:40:27array by using the print r function.
- 4:40:32For a cleaner output, I recommend
- 4:40:34echoing pre-tags around the print r
- 4:40:37function like so.
- 4:40:41They're incredibly useful for formatting
- 4:40:43arrays onto the page instead of the
- 4:40:45entire array being printed on a single
- 4:40:48line. Each item gets put on a single
- 4:40:50line. Looking closely, the null value is
- 4:40:53absent from the array. By default, the
- 4:40:56array filter function removes items with
- 4:40:59empty values like null or false.
- 4:41:01However, we may want to customize this
- 4:41:04behavior. In the resource section of
- 4:41:06this lecture, I provide a link to this
- 4:41:08function. If we look at the function
- 4:41:10definition, this function has a few
- 4:41:12parameters. I want to focus on the
- 4:41:14second parameter. Let's take a close
- 4:41:17look at the data type. It's set to
- 4:41:19callable. As we know, the callable data
- 4:41:22type is a data type for callback
- 4:41:24functions. PHP uses callback functions
- 4:41:28to allow other developers to customize
- 4:41:30the behavior of its own functions. If
- 4:41:33you were to read through the
- 4:41:34documentation, the callback function can
- 4:41:37be used for customizing the filtering
- 4:41:39behavior. Let's give it a try. Back in
- 4:41:42our script, pass in an arrow function as
- 4:41:44the second argument to the array filter
- 4:41:47function.
- 4:41:50In this example, let's try removing Bob
- 4:41:52from the array. In some cases, we may
- 4:41:55want to exclude specific users. So,
- 4:41:58filtering a specific user isn't out of
- 4:42:00the question. Behind the scenes, PHP
- 4:42:03loops through our array. Our callback
- 4:42:05function will be called on each
- 4:42:07iteration during which we can accept the
- 4:42:10user as a parameter. Let's call the
- 4:42:13parameter user.
- 4:42:16Our callback function must return a
- 4:42:18boolean value. If true is returned, the
- 4:42:22item stays in the array, otherwise it'll
- 4:42:25get removed. Let's return the following.
- 4:42:27User does not equal Bob.
- 4:42:32As long as the value is not equal to
- 4:42:34Bob, the value can stay in the array.
- 4:42:36Looking at the preview, Bob was
- 4:42:38successfully removed. One thing you'll
- 4:42:41notice is that the index between 1 and
- 4:42:44three is skipped. BHP does not reindex
- 4:42:47the array. As a mini exercise, I'll let
- 4:42:50you try to fix this problem. We already
- 4:42:52talked about a function that can be used
- 4:42:54for reindexing values. Let's move on to
- 4:42:58another function. Sometimes we may not
- 4:43:00want to filter values. However, we may
- 4:43:03want to make adjustments to each value
- 4:43:05in an array. The array map function
- 4:43:07allows us to do just that. Let's replace
- 4:43:10the array filter function with the array
- 4:43:13map function.
- 4:43:16Next, the array map function accepts the
- 4:43:19array as the second argument. So, let's
- 4:43:22swap the parameters by moving the user's
- 4:43:25array as the second argument.
- 4:43:29Afterward, change the expression to the
- 4:43:32following string to upper user.
- 4:43:36Similar to the array filter function,
- 4:43:39the array map function accepts a
- 4:43:41callback function. Every value in the
- 4:43:43array will be looped through. On each
- 4:43:45iteration, our callback function will be
- 4:43:48called with each value in the array. We
- 4:43:50can use this opportunity to modify the
- 4:43:53value. In this example, we're using a
- 4:43:55new function called string to upper.
- 4:43:58It'll convert lowercase letters into
- 4:44:00uppercase. In the preview, the names
- 4:44:02have been updated to all uppercase
- 4:44:05letters. Perfect. We're able to modify
- 4:44:07the values instead of filtering them. In
- 4:44:10some scenarios, we may want to merge two
- 4:44:13or more arrays. For example, let's say
- 4:44:16we were creating an application for
- 4:44:18tracking expenses. You may allow users
- 4:44:20to upload documents with their expense
- 4:44:23reports. Since multiple reports can be
- 4:44:25uploaded, you may have multiple arrays
- 4:44:28of data. Dealing with multiple arrays
- 4:44:30can be a hassle, so merging them into a
- 4:44:33single array is the best way to go. PHP
- 4:44:36offers a function for merging arrays
- 4:44:38called array merge. Let's replace the
- 4:44:41array map function with this function.
- 4:44:46Next, we must pass in our arrays. In the
- 4:44:49resource section of this lecture, I
- 4:44:51provide a link to the array merge
- 4:44:53function. Take a close look at the
- 4:44:55definition. Notice anything significant?
- 4:44:58The array merge function is a variatic
- 4:45:01function. It has a single parameter with
- 4:45:04the spread operator, meaning that the
- 4:45:06function can accept an unlimited number
- 4:45:08of arrays. Let's head back to our
- 4:45:11editors. Passed in the users array.
- 4:45:16Next, let's create an array on the spot.
- 4:45:19In this array, insert two users called
- 4:45:21Sam and Jessica.
- 4:45:25In the preview, Sam and Jessica have
- 4:45:27been added. Perfect. There's one last
- 4:45:30thing I want to show you. Sorting arrays
- 4:45:32is another task we may want to perform.
- 4:45:34In the resource section of this lecture,
- 4:45:37I provide a link to a function called
- 4:45:39sort. The sort function does exactly as
- 4:45:42it sounds. It'll sort the contents of an
- 4:45:45array. Before using this function,
- 4:45:47there's one thing worth highlighting.
- 4:45:49Thus far, every function has returned a
- 4:45:52brand new array. This time, the function
- 4:45:54definition tells us otherwise. Unlike
- 4:45:57before, this function returns a boolean
- 4:46:00value. So, how are we expected to grab
- 4:46:02the sorted array? Taking another look,
- 4:46:05the first parameter gives us our answer.
- 4:46:08There's an amperand character before the
- 4:46:10name. This character tells us that the
- 4:46:13parameter accepts a reference to the
- 4:46:15variable. We can safely assume that this
- 4:46:18function modifies an existing array
- 4:46:20directly. It does not store a copy of
- 4:46:23our array. This is important to
- 4:46:25understand. Not all of PHP's functions
- 4:46:28return a new value. Some of them may
- 4:46:30want to modify an existing variable. The
- 4:46:33only way to know is to read the
- 4:46:35documentation and function definition.
- 4:46:37Let's give this function a try. Back in
- 4:46:40our script, call the sort function after
- 4:46:42merging the arrays.
- 4:46:46Pass in the users array.
- 4:46:50In the preview, the array has been
- 4:46:52sorted alphabetically. In addition, the
- 4:46:54names can be found under new indexes.
- 4:46:57Previously, Jessica's index was five
- 4:47:00because she was the last name to appear
- 4:47:02in the array. This time, her index is
- 4:47:05three. Not only were the values
- 4:47:07rearranged, but the indexes were
- 4:47:09affected, too. You may not find this
- 4:47:12behavior desirable. What if we had an
- 4:47:15associative array? Our named indexes
- 4:47:17would be thrown away. In the resource
- 4:47:20section of this lecture, I provide a
- 4:47:22link to a documentation page with an
- 4:47:24official list of sorting functions.
- 4:47:26Sorting is a common action. PHP does not
- 4:47:29assume how you want to sort your values.
- 4:47:32For this reason, dozens of functions are
- 4:47:34offered for sorting arrays differently.
- 4:47:37If we scroll through the page, we'll
- 4:47:39call across a table of these functions.
- 4:47:42Some functions can sort by using the
- 4:47:44value or the key.
- 4:47:47Other functions can maintain the
- 4:47:49original key of the value or reindex the
- 4:47:52array.
- 4:47:55Lastly, functions can sort in ascending
- 4:47:58or descending order.
- 4:48:01If a function doesn't satisfy what
- 4:48:03you're looking for, PHP offers an
- 4:48:05alternative solution.
- 4:48:09We're looking for a function that sorts
- 4:48:11an array by its value in ascending order
- 4:48:14while maintaining the original key.
- 4:48:16According to the table, the a sort
- 4:48:19function seems like a perfect fit. Keep
- 4:48:21in mind, the table only provides a
- 4:48:23summary of what each function does. I
- 4:48:26highly recommend checking out the
- 4:48:28individual documentation pages for each
- 4:48:30function to truly understand what it can
- 4:48:33and can't do. Let's try using the a sort
- 4:48:37function. Back in the script, replace
- 4:48:39the sort function with the a sort
- 4:48:42function.
- 4:48:45The preview generates the same array as
- 4:48:47before. The names are sorted
- 4:48:49alphabetically. The main difference is
- 4:48:51the keys. Jessica retains her original
- 4:48:54index which was five. Everything is
- 4:48:56working as intended. Hopefully, this
- 4:48:59gives you an idea of what can be done
- 4:49:01with arrays. We can't cover every
- 4:49:03function, but we can go over the most
- 4:49:06common ones. As we progress through the
- 4:49:08course, I'll be sure to introduce
- 4:49:10additional functions. For now, these
- 4:49:13functions should suffice. In the next
- 4:49:15lecture, let's explore another feature
- 4:49:17of arrays.
- 4:49:19[Music]
- 4:49:31In this lecture, we're going to explore
- 4:49:33a feature to help us improve
- 4:49:35readability. Arrays can have dozens of
- 4:49:37values. It's common to extract a single
- 4:49:40value from an array and store it in a
- 4:49:43variable. This is known as dstructuring.
- 4:49:46Let's look at an example. In our script,
- 4:49:49let's replace the current code with an
- 4:49:51array called numbers. Fill the array
- 4:49:53with random numbers.
- 4:49:57Next, let's write the following. List a
- 4:50:00equals numbers.
- 4:50:03The list keyword can be used for
- 4:50:05grabbing items from an array. PHP will
- 4:50:08store them with the variable names
- 4:50:10written inside the list keyword. The
- 4:50:12order does matter. The first variable
- 4:50:14gets assigned the first value in the
- 4:50:17array. We can add more variables by
- 4:50:19separating them with commas. For
- 4:50:21example, I'll add a variable called B.
- 4:50:24This variable will store the second
- 4:50:26value in the array. so on and so forth.
- 4:50:29It's not required to provide a variable
- 4:50:32for each item. Let's echo the a
- 4:50:34variable.
- 4:50:37As you can see, we get the first number
- 4:50:39from the array. There's an alternative
- 4:50:41syntax for dstructuring an array. Rather
- 4:50:44than wrapping the variable with the list
- 4:50:46keyword, we can use a pair of square
- 4:50:48brackets.
- 4:50:51This shorthand syntax produces the same
- 4:50:54results. The last thing I want to show
- 4:50:56you is dstructuring arrays with key
- 4:50:59names. Let's update the array by setting
- 4:51:01the index of the first variable to
- 4:51:04example.
- 4:51:06Next, in our dstructuring, we can grab
- 4:51:09the variable by referencing the key name
- 4:51:12followed by a fat arrow and variable
- 4:51:14name.
- 4:51:17If we decide to dstructure variables by
- 4:51:20specific keys, all variables in the
- 4:51:23dstructuring must also use specific
- 4:51:25keys. So for the B variable, set the key
- 4:51:29to zero.
- 4:51:32In the preview, the variable continues
- 4:51:34to work. Dstructuring can be handy
- 4:51:37because it allows us to assign a
- 4:51:39specific variable name to a value in an
- 4:51:42array. It makes our code readable and
- 4:51:44concise. In the next lecture, let's
- 4:51:47discuss one final topic for this
- 4:51:49section.
- 4:51:50[Music]
- 4:52:02In this lecture, let's talk about the
- 4:52:04file system. In the resource section of
- 4:52:07this lecture, I provide a link to PHP's
- 4:52:10official list of file system functions.
- 4:52:12PHP offers dozens of functions for
- 4:52:15interacting with the file system. You
- 4:52:17can perform operations from reading the
- 4:52:20file size to creating new files. Let's
- 4:52:22give some of these functions a try.
- 4:52:25Switch over to replet. Replace the
- 4:52:27contents of the file with a variable
- 4:52:29called directory. Its value will be a
- 4:52:32function called scan directory.
- 4:52:36Typically, you're going to want to scan
- 4:52:39a directory for a list of files. The
- 4:52:41scan directory function performs this
- 4:52:44exact action. There's one argument,
- 4:52:46which is the directory to scan. Luckily,
- 4:52:49we don't have to guess the directory
- 4:52:51we're currently in. We can use the
- 4:52:53directory magic constant we learned
- 4:52:55about in an earlier lecture.
- 4:52:59As a reminder, this constant points to
- 4:53:02the directory of the file it's being
- 4:53:04used in. The return value of this
- 4:53:07function is an array. So let's print the
- 4:53:10contents of the directory variable with
- 4:53:12the print r function.
- 4:53:16In the preview, we're given a list of
- 4:53:18files. You should see most of our files,
- 4:53:21including the hidden files created by
- 4:53:23replet. There are two items in the array
- 4:53:26that may be mysterious. Looking closely,
- 4:53:29the first two items of the array are a
- 4:53:31dot and two dots. Every time you scan a
- 4:53:34directory, every directory is going to
- 4:53:37have these two items. A single dot
- 4:53:39refers to the current directory, whereas
- 4:53:41the two dots refer to the parent
- 4:53:44directory. Keep this in mind as you're
- 4:53:46not just going to be presented with the
- 4:53:48files in a directory. These items will
- 4:53:51always appear in your array. Moving
- 4:53:53along, let's try creating a new
- 4:53:55directory. Replace the current code with
- 4:53:58a function called mk directory.
- 4:54:03The letters mk are short for make. This
- 4:54:06function accepts the name of the
- 4:54:08directory as a string. Let's set the
- 4:54:10name to fu.
- 4:54:14If we run the file, nothing appears in
- 4:54:16the preview. However, if we look at the
- 4:54:19file tree from the sidebar, a new folder
- 4:54:22has been created called fu. Next, let's
- 4:54:25try replacing the function with a
- 4:54:27function called rm directory.
- 4:54:32RM being short for remove. After
- 4:54:35refreshing the page, the food directory
- 4:54:37disappears. We can use this function for
- 4:54:40deleting directories. There is one thing
- 4:54:42to keep in mind when deleting
- 4:54:44directories. The directory must be empty
- 4:54:46before being deleted. If we attempt to
- 4:54:49delete a directory with files, PHP
- 4:54:51throws an error. So keep that in mind
- 4:54:54when deleting directories. Let's start
- 4:54:56exploring functions for interacting with
- 4:54:59files. Before working with a file, you
- 4:55:01should check that it exists. Otherwise,
- 4:55:04any action you perform may not work as
- 4:55:07expected. In our script, replace the
- 4:55:09function with a conditional statement.
- 4:55:12The condition will be a function called
- 4:55:14file exists.
- 4:55:17The file exists function can be used to
- 4:55:20check for the existence of a file. We
- 4:55:23must pass in the name of the file as a
- 4:55:25string. Unlike before, we don't need to
- 4:55:28provide a full system path. It's
- 4:55:30perfectly fine to write a path relative
- 4:55:32to the current directory. For example,
- 4:55:35let's try checking if a file called
- 4:55:38example.txt
- 4:55:40exists.
- 4:55:42If it does, we'll echo a message saying
- 4:55:45file found
- 4:55:49in the preview. Nothing appears because
- 4:55:51we don't have a file called example.txt.
- 4:55:54Let's try creating this file from within
- 4:55:57our project. Make sure it's in the same
- 4:55:59directory as the index.php file.
- 4:56:04Next, if we refresh the page, the
- 4:56:07message appears. Great. We're able to
- 4:56:10verify that a file exists. Let's try
- 4:56:13checking the file size. Replace the
- 4:56:15expression for the echo statement with
- 4:56:17the file size function.
- 4:56:21As you might expect, this function
- 4:56:23returns the size of a file. Let's pass
- 4:56:26in the example.txt
- 4:56:28file.
- 4:56:31In the preview, the number zero gets
- 4:56:33rendered. That's to be expected since I
- 4:56:36didn't write anything inside the text
- 4:56:38file. Let's try writing content into the
- 4:56:41file. PHP has a simple function to
- 4:56:44perform this task. After the echo
- 4:56:46statement, run a function called fileput
- 4:56:49contents.
- 4:56:52Two pieces of information must be passed
- 4:56:54in. Firstly, we must provide the file
- 4:56:57name, which is example.txt.
- 4:57:02Next, we must provide the contents we'd
- 4:57:05like to insert into the file. Let's
- 4:57:07write hello world.
- 4:57:10Lastly, just to verify that our function
- 4:57:13worked, let's echo the file size again.
- 4:57:18If we refresh the page, everything seems
- 4:57:21to have worked. But there's just one
- 4:57:23problem. The number zero gets outputed
- 4:57:25twice. At first glance, it can seem like
- 4:57:28PHP didn't update the example.php file.
- 4:57:32Let's take a peek inside the example.txt
- 4:57:35file. Surprisingly, the value has passed
- 4:57:38into our function. So why is PHP saying
- 4:57:41that the file size is zero bytes? The
- 4:57:44reason is simple. Behind the scenes, PHP
- 4:57:47caches the results returned by some of
- 4:57:50the file system functions. By calling
- 4:57:52the file size function before updating
- 4:57:55the files contents, the result gets
- 4:57:57cached. If we attempt to call the file
- 4:58:00size function again, we're going to get
- 4:58:02the previous result. PHP does not bother
- 4:58:06rechecking the file for a new file size.
- 4:58:08Checking file data can be an expensive
- 4:58:11action. For performance reasons, PHP
- 4:58:14reuses the original result. This
- 4:58:16behavior is beneficial to us, but in
- 4:58:19this case, it's a problem. Luckily, we
- 4:58:21can reset the results in the text file.
- 4:58:24Clear the contents.
- 4:58:28Back in the PHP file, after updating the
- 4:58:30files contents, call the clear stat
- 4:58:33cache function.
- 4:58:37The clear stat cache function clears the
- 4:58:40cache. Therefore, PHP attempts to reread
- 4:58:43the file size. In the preview, zero gets
- 4:58:46rendered in the output, followed by 11,
- 4:58:49thus indicating that the file size was
- 4:58:51updated. Perfect. This is something to
- 4:58:54keep in mind. If you're constantly
- 4:58:56reading file data, you may be returned
- 4:58:59older results instead of newer results.
- 4:59:01You can avoid this behavior by calling
- 4:59:04the clear stat cache function. Let's
- 4:59:07explore one more function. Instead of
- 4:59:09outputting file size, let's grab the
- 4:59:11contents of the file. For the second
- 4:59:14echo statement, replace the file size
- 4:59:16function with the file get contents
- 4:59:19function.
- 4:59:22In the preview, the text from the file
- 4:59:24appears. It's as simple as that. The
- 4:59:27file get contents function just accepts
- 4:59:30the name of the file. It'll return the
- 4:59:32file contents as a string. Hopefully,
- 4:59:35this gives you a better idea of how we
- 4:59:37can work with files in PHP. That
- 4:59:40concludes this section. We've explored
- 4:59:43dozens of PHP's features. Now that we
- 4:59:46have a solid grasp of PHP, it's time to
- 4:59:49take things to the next level. We're
- 4:59:51going to start talking about
- 4:59:52objectoriented programming. Before we
- 4:59:55do, I have a few challenges for you to
- 4:59:57take on to reinforce what we've learned.
- 5:00:00So, when you're ready, I'll see you in
- 5:00:02the next section.
- 5:00:03[Music]
- 5:00:14Nice job. You've just taken your first
- 5:00:15steps into PHP development. You now know
- 5:00:18how to use variables and data, write
- 5:00:20logic to control your code, use the
- 5:00:22official PHP documentation to keep
- 5:00:24learning on your own. These are the
- 5:00:26foundations that you need to start
- 5:00:27building realworld projects. But PHP
- 5:00:30goes much deeper than that. So, if
- 5:00:32you're ready to start diving into OOP,
- 5:00:34authentication, databases, and even
- 5:00:36build a full expense tracking app, then
- 5:00:38check out Louisa's full PHP boot camp
- 5:00:40course. It's designed to take you from
- 5:00:42beginner to building professional-grade
- 5:00:44applications that you can put on your
- 5:00:46portfolio and will help you stand out to
- 5:00:48land more interviews and get hired. And
- 5:00:51if you decide to join the Zero to
- 5:00:52Mastery Academy, you'll not only get
- 5:00:54full access to Louisa's full PHP boot
- 5:00:56camp course, but every single other
- 5:00:58course that ZTM has to offer. and you'll
- 5:01:01get access to our membersonly Discord
- 5:01:03channel where you can get 247 priority
- 5:01:05support to help you reach your goals
- 5:01:07faster. If you've made it this far,
- 5:01:09please help me show Louise some love by
- 5:01:11dropping this video a like and leaving a
- 5:01:13comment with your questions, thoughts,
- 5:01:14or feedback down below. That's it for me
- 5:01:17and I look forward to seeing you inside
- 5:01:19the Zero to Mastery
About this transcript
This page contains the full transcript of PHP Crash Course: Learn Modern PHP & Master the Fundamentals (Step-By-Step) by Zero To Mastery, generated from the public captions YouTube serves with the video. The transcript has 43,360 words across 6,906 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.