YouTube2Text

PHP Crash Course: Learn Modern PHP & Master the Fundamentals (Step-By-Step) — Transcript

by Zero To Mastery · 43,360 words · 6,906 segments · language en · Watch on YouTube

Full transcript

  1. 0:04They say PHP is dead, but here's the
  2. 0:06thing. It still powers over 70% of the
  3. 0:08web. From WordPress to modern web apps,
  4. 0:11PHP is still very much alive, and
  5. 0:14learning it is a great way to break into
  6. 0:15back-end development. And that's what
  7. 0:17this PHP crash course is all about,
  8. 0:19giving you a practical, modern
  9. 0:21introduction to PHP. By the end, you'll
  10. 0:24have the skills to build your own PHP
  11. 0:26applications. Your instructor is Luis
  12. 0:28Ramirez, a software engineer with over a
  13. 0:31decade of experience building secure,
  14. 0:33scalable, productionready applications.
  15. 0:35Luis has taught over thousands of
  16. 0:37students how to not just write PHP,
  17. 0:39build their own apps, and help them get
  18. 0:40hired, but his students truly understand
  19. 0:42the why behind the code, setting them
  20. 0:45apart from many PHP developers. I almost
  21. 0:47forgot, we're giving away a huge bonus
  22. 0:50for anyone taking this crash course.
  23. 0:51free access to Zero to Mastery's Discord
  24. 0:54community where you can hang out with
  25. 0:55Luis mentors and fellow students.
  26. 0:58Normally, this is reserved for ZTM
  27. 1:00Academy members, but for a limited time,
  28. 1:02we're giving you access so that you
  29. 1:03don't have to learn alone and that you
  30. 1:05can get direct help from ZTM mentors and
  31. 1:08instructors. The invite link is in the
  32. 1:10description down below. So, hit pause,
  33. 1:12go find that link, join the server, say
  34. 1:15hi, and then come on back. All done. All
  35. 1:18right, let me hand it over to Luis so
  36. 1:19that you can start learning PHP. Enjoy.
  37. 1:22[Music]
  38. 1:31[Music]
  39. 1:33PHP is one of the best languages you can
  40. 1:35learn. Now, hold on a moment. That's a
  41. 1:37controversial statement. A quick online
  42. 1:40search yields hundreds of results
  43. 1:42declaring PHP a dead language. Over the
  44. 1:45years, many programmers have developed a
  45. 1:47misconception about PHP. Let's examine
  46. 1:50the facts to decide whether PHP is a
  47. 1:53perfect fit for you. First and foremost,
  48. 1:5580% of sites are powered by PHP. Can you
  49. 1:58believe it? That pretty much means that
  50. 2:01eight out of the 10 websites you visit
  51. 2:03are using PHP under the hood, thus
  52. 2:05solidifying itself as a reliable
  53. 2:07language for the web. On top of being a
  54. 2:10popular language, performance is another
  55. 2:12strong point. Over the years,
  56. 2:14maintainers have taken every step to
  57. 2:16improve the speed of PHP. Without a
  58. 2:19doubt, those efforts have paid off. Apps
  59. 2:21have seen significant improvement in
  60. 2:23performance with each release. Lastly,
  61. 2:26new features are constantly introduced
  62. 2:29in the language. Since PHP has wide
  63. 2:31adoption, the PHP team can tailor and
  64. 2:34fine-tune the language for the
  65. 2:36community. They've introduced features
  66. 2:37that can be found in most modern
  67. 2:39languages. These features can help us
  68. 2:41write better, more efficient code. There
  69. 2:44has to be a catch, right? After all,
  70. 2:47developers are avoiding PHP for a
  71. 2:49reason. PHP is one of the most
  72. 2:51beginnerfriendly languages available.
  73. 2:53Within a weekend, deploying a site from
  74. 2:55development to production is possible.
  75. 2:58Unfortunately, this aspect of the
  76. 3:00language is a double-edged sword.
  77. 3:02Security vulnerabilities can easily be
  78. 3:04introduced into a site. As a beginner,
  79. 3:07you should always take the proper
  80. 3:08precautions for writing secure code.
  81. 3:11More often than not, beginners tend to
  82. 3:13overlook basic security features. Worst
  83. 3:16of all, terrible programming practices
  84. 3:18can easily plague an application.
  85. 3:20Stealing an application should always be
  86. 3:22one of your top priorities. That's
  87. 3:24difficult to do when the code is poorly
  88. 3:26written. Luckily, you're taking this
  89. 3:29course. My goal for this course is to
  90. 3:31help you become a proficient PHP
  91. 3:33developer. Unlike other courses that
  92. 3:35teach you the basics and send you on
  93. 3:37your way, this course covers a wide
  94. 3:39variety of topics starting from the
  95. 3:41basics of the language to covering
  96. 3:43advanced features such as security and
  97. 3:46MVC architecture. If you're interested
  98. 3:49in a complete overview, I highly
  99. 3:51recommend checking out the course
  100. 3:52description. As we progress through the
  101. 3:55course, I'll be sure to point out best
  102. 3:57practices. This way, you can avoid
  103. 3:59common pitfalls most beginners fall into
  104. 4:02during their journey. I know you're
  105. 4:03antsy to get started, but there's one
  106. 4:05thing you should know before digging
  107. 4:07into the course. This course assumes you
  108. 4:09have knowledge of HTML and CSS. Our
  109. 4:13focus is primarily going to be on PHP.
  110. 4:15To save time, I'll provide most of the
  111. 4:18HTML and CSS without reviewing every
  112. 4:21single line of code. You're expected to
  113. 4:23understand basic HTML syntax. In
  114. 4:26addition, JavaScript would be nice to
  115. 4:28know, but not required. JavaScript is
  116. 4:31not a language we'll be using. heavily.
  117. 4:33If you know JavaScript, that'll come in
  118. 4:35handy. Otherwise, don't worry about it.
  119. 4:38All right, it's time to start learning.
  120. 4:40I hope you're excited. It's going to be
  121. 4:42a long but worthwhile journey. When
  122. 4:44you're ready, I'll see you in the next
  123. 4:46lecture.
  124. 4:47[Music]
  125. 4:58In this lecture, we're going to start
  126. 5:00preparing an environment for PHP. It's
  127. 5:02going to be easier than you think. But
  128. 5:04what is an environment? Why is it
  129. 5:06important to have one? Let's demystify
  130. 5:09this concept before taking a step
  131. 5:11further. Suppose we were gardening. What
  132. 5:13does a garden need? There needs to be
  133. 5:15soil, sunlight, and water. Without the
  134. 5:18proper environment, our plants would not
  135. 5:20be able to flourish. It's absolutely
  136. 5:23critical to cultivate land for growing
  137. 5:25plants and food. The concept of
  138. 5:28preparing an environment for gardening
  139. 5:30can be applied to the programming world.
  140. 5:32Applications need the proper environment
  141. 5:34for running code. In the programming
  142. 5:36world, an environment is the machine
  143. 5:38where the application runs. In most
  144. 5:41cases, applications rely on additional
  145. 5:43software. Without the proper tools or
  146. 5:46programs, our applications won't work.
  147. 5:49Generally speaking, there are two types
  148. 5:51of environments which are local and
  149. 5:53production environments. A local
  150. 5:56environment refers to the setup you have
  151. 5:58on your personal computer or local
  152. 6:00machine where you can write, test, and
  153. 6:02run your code. Local environments are
  154. 6:05primarily used for development and
  155. 6:07testing purposes. This allows you, the
  156. 6:10developer, to break things without
  157. 6:12disrupting your users. You can freely
  158. 6:14modify and make changes before shipping
  159. 6:17a program to the world. On the other
  160. 6:19hand, a production environment is a live
  161. 6:22environment where your code is actually
  162. 6:24deployed and running for end users. A
  163. 6:26production environment is typically
  164. 6:28hosted on a remote server. It is
  165. 6:31designed to handle the demands of
  166. 6:33realworld usage including security,
  167. 6:36scalability, and reliability. The
  168. 6:38production environment is also
  169. 6:40monitored, managed, and maintained by a
  170. 6:43team of professionals to ensure optimal
  171. 6:45performance. It's not the place to test
  172. 6:47our code. If we accidentally upload
  173. 6:50errorprone code, our users will be
  174. 6:52affected by our actions.
  175. 6:56As you can imagine, we will be working
  176. 6:58in a local environment. Setting up an
  177. 7:00environment can take a bit of time.
  178. 7:02Luckily, the PHP community has taken the
  179. 7:05time to provide solutions for quickly
  180. 7:07stirring up PHP environments. In the
  181. 7:10resource section of this lecture, I
  182. 7:12provide links to the most popular
  183. 7:14solutions. Developers have a plethora of
  184. 7:17options for installing PHP. There are
  185. 7:20dozens of programs online to easily
  186. 7:22prepare an environment for running PHP
  187. 7:25on a local machine. You may have
  188. 7:27encountered them such as XamP,
  189. 7:30M or Laragon. While these programs are
  190. 7:34helpful, we're going to take a different
  191. 7:36approach. One of the links in the
  192. 7:38resource section takes you to a site
  193. 7:40called Replet. Replet is a service for
  194. 7:43creating environments for various
  195. 7:45programming languages. Best of all, our
  196. 7:48environments are completely accessible
  197. 7:49from the browser. We do not need to
  198. 7:52install programs on our machine to run
  199. 7:54PHP. I find that Replet is the easiest
  200. 7:57solution for beginners to get started
  201. 7:59with PHP. Here's what I want you to do.
  202. 8:02Create an account on Replet and log in
  203. 8:04into the dashboard. I'm going to quickly
  204. 8:06sign into my account. I'll see you in a
  205. 8:09moment.
  206. 8:12All right. After signing up for an
  207. 8:14account, let's create an environment.
  208. 8:17Keep in mind environments created on
  209. 8:19Replet are meant to be for development
  210. 8:21purposes. The applications built on this
  211. 8:24site are not meant to be publicly hosted
  212. 8:26for the world. In the future, we'll
  213. 8:28discuss some options for publicly
  214. 8:30hosting your app. For now, let's shift
  215. 8:33our focus to creating a new environment.
  216. 8:36At the top left corner, there's a button
  217. 8:38called create. Click on it. A pop-up is
  218. 8:41going to appear to ask us a few
  219. 8:43questions. Firstly, we must select a
  220. 8:46template. As I mentioned earlier, Replet
  221. 8:49supports multiple programming languages.
  222. 8:51From the template option, we can search
  223. 8:54for PHP. A few options may appear. The
  224. 8:57option we're interested in is called PHP
  225. 9:00web server. Make sure to select this
  226. 9:02option. Otherwise, you may encounter
  227. 9:05errors when going through the course.
  228. 9:07Next, we must provide a name for our new
  229. 9:10project. Let's call it PHP fundamentals.
  230. 9:15Lastly, let's create the project. Replet
  231. 9:18starts preparing our environment on our
  232. 9:21behalf. This process may take a few
  233. 9:23moments. Just be patient.
  234. 9:27After all has been said and done, our
  235. 9:29environment is ready. Let's make a few
  236. 9:31tweaks before writing code. At the
  237. 9:34bottom left corner, there's a list of
  238. 9:35tools available. From this section,
  239. 9:38there's an option to modify the settings
  240. 9:40of the project's code editor. In my
  241. 9:43opinion, the font size is too small. You
  242. 9:45may feel the same. On the settings tab,
  243. 9:48there's an option for enlarging the
  244. 9:49font. Select a font size that's suitable
  245. 9:52for your needs. Hopefully, you can read
  246. 9:55the code on my screen. Great. We're
  247. 9:57ready to run the project. At the top of
  248. 9:59the page, click the run button.
  249. 10:04Behind the scenes, Replet takes the time
  250. 10:06to launch our PHP application. After a
  251. 10:09few moments, a new tab appears for
  252. 10:12previewing our application. The message
  253. 10:14hello world renders on our screens.
  254. 10:17Isn't that awesome? With a few clicks,
  255. 10:19we've got a working environment. Below
  256. 10:21the preview, there's a section called
  257. 10:23console. The console is a feature we
  258. 10:26don't need to worry about. It's taking
  259. 10:28up unnecessary room on our screens.
  260. 10:31Fortunately, we can click on the web
  261. 10:33view portion to drag it around. If we
  262. 10:36hover this section of the editor with
  263. 10:38the console, it'll get merged with the
  264. 10:40console as a tab. I recommend moving
  265. 10:43things around to reduce clutter. All we
  266. 10:45need is the file tree, code editor, and
  267. 10:48preview. Everything else will be
  268. 10:50explored as we learn more about PHP.
  269. 10:54If you have experience with PHP, you may
  270. 10:57be curious as to why I haven't mentioned
  271. 11:00Docker. Docker is a tool for managing
  272. 11:02environments on your machine.
  273. 11:04Professional PHP developers use Docker
  274. 11:07for their projects. So, why aren't we
  275. 11:09using it? The reason is simple. Docker
  276. 11:12is very complicated for beginners. I
  277. 11:15want us to focus on the basics of PHP
  278. 11:18before diving into larger, more complex
  279. 11:20tools. I consider Docker to be a great
  280. 11:23option. For now, Replet will suffice.
  281. 11:28It's time to finally start coding. For
  282. 11:30the rest of this course, I'm going to
  283. 11:32assume you have the PHP server running
  284. 11:34unless I state otherwise. When you're
  285. 11:37ready, I'll see you in the next section.
  286. 11:40[Music]
  287. 11:50In this lecture, we're going to dive
  288. 11:52into programming theory. More precisely,
  289. 11:55we're going to talk about what PHP is
  290. 11:58doing when we write code. To kick this
  291. 12:00discussion off, I think we should take
  292. 12:02the time to define PHP. That's going to
  293. 12:05make sure we're all on the same page.
  294. 12:07Here's the best definition of PHP
  295. 12:10available. PHP is an open-source
  296. 12:13generalpurpose scripting language for
  297. 12:15developing web applications. Wo, what
  298. 12:18does all that mean? Let's slowly unpack
  299. 12:20that definition to fully grasp PHP.
  300. 12:24First and foremost, open-source
  301. 12:26software, OSS for short, refers to code
  302. 12:29that has been freely released to the
  303. 12:31public. Anyone can study, modify, and
  304. 12:34redistribute the software. Collaborators
  305. 12:37from all over the world have voluntarily
  306. 12:39contributed their time to improving PHP.
  307. 12:42It's one of the largest open-source
  308. 12:44software projects in the world.
  309. 12:47You may be curious, does this mean that
  310. 12:50you can download the original code for
  311. 12:52PHP and modify it? The answer is yes,
  312. 12:54you can. In the resource section of this
  313. 12:57lecture, I provide a link to the
  314. 12:59official PHP GitHub account. On this
  315. 13:02page, you're going to find various links
  316. 13:04related to the PHP project. Everything
  317. 13:07is available to the public free of
  318. 13:09charge. If you don't like where PHP is
  319. 13:12heading, you're more than welcome to
  320. 13:13modify the language to your heart's
  321. 13:15content. However, most people are happy
  322. 13:18with PHP, so that probably won't be
  323. 13:20necessary. Regardless, it's still good
  324. 13:23to know where PHP is discussed and
  325. 13:26maintained.
  326. 13:28Moving forward, let's keep dissecting
  327. 13:30the definition of PHP. I mentioned that
  328. 13:33PHP is a generalpurpose language. A
  329. 13:36generalpurpose language is a programming
  330. 13:38language that can be utilized to develop
  331. 13:40different kinds of programs. Originally,
  332. 13:43PHP was introduced as a language for
  333. 13:45developing web applications. As time
  334. 13:48passed, developers discovered it could
  335. 13:50be used for other things. It's possible
  336. 13:53to develop command line scripts, desktop
  337. 13:55applications, image processing
  338. 13:57libraries, and so much more. Programming
  339. 14:00languages are considered to be general
  340. 14:02purpose when they can be used to solve a
  341. 14:04wide array of problems. For all intents
  342. 14:07and purposes, this course primarily
  343. 14:09focuses on web development with PHP.
  344. 14:12Other areas of software development
  345. 14:14aren't explored. you're more likely to
  346. 14:17be hired as a PHP web developer than a
  347. 14:19PHP desktop application developer.
  348. 14:22Lastly, PHP is considered to be a
  349. 14:25scripting language because it's
  350. 14:26interpreted. Technically, it's not wrong
  351. 14:29to call it a programming language. After
  352. 14:31all, all scripting languages are
  353. 14:33programming languages, but the reverse
  354. 14:36isn't true. Not all programming
  355. 14:38languages are scripting languages. That
  356. 14:40leads us to the question, what does it
  357. 14:42mean when a programming language is
  358. 14:44interpreted? If you've ever watched a
  359. 14:46movie or TV show about computers, you
  360. 14:49may have seen something like this. What
  361. 14:51you're being shown is called binary code
  362. 14:53or machine code. Binary code is the only
  363. 14:56language that computers can understand.
  364. 14:59As you can imagine, reading binary code
  365. 15:01is not the most ideal situation to find
  366. 15:04yourself in. For this reason,
  367. 15:06programming languages were introduced to
  368. 15:08improve readability. On the other hand,
  369. 15:11a new problem arises from using
  370. 15:13programming languages. Since computers
  371. 15:16can't understand programming languages,
  372. 15:18code must be transformed into machine
  373. 15:20code. This is where a program called a
  374. 15:23compiler comes into play. The job of a
  375. 15:25compiler is to transform our code into
  376. 15:28machine code. After our code has been
  377. 15:31transformed, the instructions written
  378. 15:33from our program can be executed by a
  379. 15:35machine. This concept is not exclusive
  380. 15:38to PHP. Other programming languages
  381. 15:40share this feature, whether it's Python,
  382. 15:43C++, or Rust. So, what does this have to
  383. 15:46do with an interpreter? There are two
  384. 15:49main approaches to generating machine
  385. 15:51code. You can use either a compiler or
  386. 15:54an interpreter. A compiler and an
  387. 15:56interpreter are tools used to translate
  388. 15:59computer code into a form that a
  389. 16:01computer can execute. The main
  390. 16:03difference between the two is how they
  391. 16:05do it. A compiler translates the entire
  392. 16:08code of a program into machine code in
  393. 16:11one go and saves it as an executable
  394. 16:13file. This file can then be run as many
  395. 16:16times as needed without recompiling it.
  396. 16:19An interpreter, on the other hand,
  397. 16:21executes the code line by line,
  398. 16:23translating each line of code into
  399. 16:25machine code and then executing it
  400. 16:27immediately. It does not generate an
  401. 16:30executable file. Here's another way of
  402. 16:32thinking about it. A compiler is like a
  403. 16:34translator who translates a book into
  404. 16:37another language and then gives you the
  405. 16:38translated book to read. An interpreter
  406. 16:41is like a translator who translates a
  407. 16:43sentence and then reads it to you
  408. 16:45immediately. So you may be wondering, is
  409. 16:48PHP compiled or interpreted? PHP is
  410. 16:52interpreted. Programming languages that
  411. 16:54are interpreted are considered to be
  412. 16:56scripting languages. Therefore, PHP is a
  413. 16:59scripting language. With that being
  414. 17:02said, there are debates about which
  415. 17:04approach is better. Should programmers
  416. 17:06use compiled languages or interpreted
  417. 17:09languages? Those discussions are beyond
  418. 17:11the scope of this course. All you need
  419. 17:13to know is that PHP is interpreted. By
  420. 17:16installing PHP, we have access to the
  421. 17:19PHP interpreter for transforming our
  422. 17:22code into machine code. The next step in
  423. 17:24our journey is to start using the PHP
  424. 17:26interpreter. In the next lecture, let's
  425. 17:29start writing our first PHP script to
  426. 17:31run it through the PHP interpreter.
  427. 17:35[Music]
  428. 17:45In this lecture, we're going to execute
  429. 17:47our first PHP script. This means we'll
  430. 17:51create a PHP file, run it through the
  431. 17:53PHP interpreter, and view the output.
  432. 17:56The question is, how can we use the PHP
  433. 17:59interpreter? We're going to use a server
  434. 18:01for performing this job. Often times, I
  435. 18:04find that beginners confuse web servers
  436. 18:06and the PHP interpreter. The job of the
  437. 18:09PHP interpreter is to transform our code
  438. 18:12into machine code. On the other hand,
  439. 18:15the job of a web server is to deliver
  440. 18:17files. To better understand, let's look
  441. 18:19at an example. In a real environment, we
  442. 18:22may have dozens of files. Servers may
  443. 18:25contain a mixture of HTML, CSS, and
  444. 18:28image files. In our browsers, we have
  445. 18:31the power to request specific files from
  446. 18:33a server. For example, let's say we
  447. 18:36wanted the about.html
  448. 18:38file. A request from the browser is sent
  449. 18:41to the web server. The job of the web
  450. 18:43server is to find the file specified in
  451. 18:46the URL. If a file is found, the file
  452. 18:49gets sent back to the browser. Lastly,
  453. 18:52the browser renders the file on the
  454. 18:54screen. Overall, it's a straightforward
  455. 18:56process. However, there's one problem
  456. 18:58with this process. Websites that deliver
  457. 19:01the same files to the browser are known
  458. 19:04as static websites. It doesn't matter
  459. 19:06how many times visitors refresh the
  460. 19:08page, they're delivered the same files.
  461. 19:11Nowadays, most websites are dynamic.
  462. 19:14Rather than delivering the same page,
  463. 19:16pages can be tailored to our users to
  464. 19:18provide a unique experience. Websites
  465. 19:21that generate unique content are known
  466. 19:23as dynamic websites. Web servers can be
  467. 19:26configured to process PHP files. For
  468. 19:29example, let's say our server has PHP
  469. 19:32files. In the browser, we can request a
  470. 19:35PHP file from the server. Our server
  471. 19:38searches for the file to deliver to the
  472. 19:40browser. So far, the process is very
  473. 19:43similar to static websites. Afterward,
  474. 19:46things begin to take an alternative
  475. 19:48route. Web servers don't immediately
  476. 19:50send the PHP file back to the browser.
  477. 19:53Instead, servers are capable of running
  478. 19:56the file through the PHP interpreter.
  479. 19:58We'll wait for the interpreter to
  480. 20:00generate HTML. Once an HTML page has
  481. 20:04been created, the file gets sent to the
  482. 20:06browser to be rendered on the visitor's
  483. 20:08machine. Understanding this process is
  484. 20:11absolutely crucial. PHP does not run in
  485. 20:14the browser. Servers are meant to
  486. 20:16execute and handle the output of a PHP
  487. 20:18file.
  488. 20:21Fortunately for us, we're using Replet.
  489. 20:23Behind the scenes, a server has been
  490. 20:26configured to deliver files. At the same
  491. 20:28time, if we request a PHP file, it'll be
  492. 20:32processed through the PHP interpreter.
  493. 20:34Let's try demoing this process. In the
  494. 20:37file tree, open the index.php file. PHP
  495. 20:41files must have the PHP extension.
  496. 20:44Otherwise, web servers will not run the
  497. 20:47file through the PHP interpreter. If the
  498. 20:50server encounters a nonPHP file, it'll
  499. 20:53automatically get sent to the browser
  500. 20:55without further processing. Inside this
  501. 20:58file, there may be initial code. It's
  502. 21:00completely fine to leave the file empty.
  503. 21:03We're not going to need the starter code
  504. 21:05provided by Replet. Now, the question
  505. 21:07is, what kind of code can be written
  506. 21:10from within a PHP file? Believe it or
  507. 21:13not, we can write valid HTML code. All
  508. 21:16HTML features are completely supported
  509. 21:18in PHP files. After all, PHP files get
  510. 21:22transformed into HTML files. It only
  511. 21:25makes sense that HTML syntax is
  512. 21:28supported. For example, let's try adding
  513. 21:31a pair of H1 tags with the message hello
  514. 21:34world.
  515. 21:36Original, I know, but just keep
  516. 21:39following along. After adding the H1
  517. 21:41tag, we must refresh the preview by
  518. 21:44pressing the refresh icon. Make sure
  519. 21:46you're refreshing the preview, not the
  520. 21:48browser. If you were to refresh the
  521. 21:50browser, the entire editor gets
  522. 21:52reloaded. I know it can be annoying to
  523. 21:54have to constantly refresh the preview.
  524. 21:57There's a shortcut for forcing a refresh
  525. 21:59on your keyboard. You can press controls
  526. 22:02shortcut to save the file. For Mac
  527. 22:04users, this command would be commands.
  528. 22:07By default, Replet autos saves your
  529. 22:10files. If you were to trigger the save
  530. 22:12action, the preview automatically
  531. 22:14refreshes. It's a simple trick that you
  532. 22:17should remember going forward. As you
  533. 22:19can see, our message appears in the
  534. 22:20preview. Cool, right? As a simple
  535. 22:23exercise, let's try creating another PHP
  536. 22:26file. In the file tree, create a new
  537. 22:28file called about.php.
  538. 22:34Once again, make sure the file has the
  539. 22:36PHP extension. Otherwise, our code won't
  540. 22:39be processed through the PHP
  541. 22:41interpreter. Next, inside the about.php
  542. 22:45file, let's write a simple message to
  543. 22:47help us identify the file in the
  544. 22:49browser.
  545. 22:52So far so good. Now, the question is,
  546. 22:55how can we preview the contents of the
  547. 22:57about.php file? Our preview continues to
  548. 23:00display the message from the index.php.
  549. 23:03PHP file. To view the contents of the
  550. 23:05about file, we must update the URL.
  551. 23:08Unfortunately, this isn't possible
  552. 23:11directly from the preview. We must open
  553. 23:13a new tab to view this information. On
  554. 23:16the right side of the web view tab,
  555. 23:18there's a button for opening the site in
  556. 23:20a new tab. Click on it.
  557. 23:24As you can see, Replet provides a free
  558. 23:27URL for previewing our site. At the end
  559. 23:30of the URL, add the following about.php.
  560. 23:36After inputting this path, the contents
  561. 23:38of the about file are rendered. Voila,
  562. 23:41we've successfully created another page.
  563. 23:44You may be wondering, how does the
  564. 23:46server know to deliver the index.php
  565. 23:48file when a file is not specified? If I
  566. 23:51were to remove the file name from the
  567. 23:53path, the contents of the index.php
  568. 23:56appear. Strange, right? Well, not
  569. 23:59really. By default, most web servers are
  570. 24:02configured to deliver an index.php
  571. 24:05or index.html
  572. 24:07file to the browser if a file is not
  573. 24:09specified. This is why we're able to
  574. 24:11view the contents of the index.php file.
  575. 24:15That's about it. So far, PHP doesn't
  576. 24:18seem all that different from HTML. Don't
  577. 24:21worry, in the next set of lectures,
  578. 24:23we're going to get an opportunity to
  579. 24:25generate dynamic content. When you're
  580. 24:27ready, I'll see you in the next one.
  581. 24:29[Music]
  582. 24:40In this lecture, we're going to start
  583. 24:42entering PHP mode from our files.
  584. 24:45Starting from where we left off in the
  585. 24:47previous lecture, we're going to
  586. 24:48continue modifying the index.php file.
  587. 24:52Actually, we're not going to need to
  588. 24:54view other files for a while. To reduce
  589. 24:57distraction, I recommend closing the
  590. 24:59sidebar by pressing the button in the
  591. 25:01top left corner.
  592. 25:05Closing the sidebar leaves us with the
  593. 25:07code editor and preview. Awesome. Now,
  594. 25:10what am I talking about when I say PHP
  595. 25:12mode? Well, if we were to write HTML
  596. 25:15inside a PHP file, the PHP interpreter
  597. 25:18does not process the HTML portion of our
  598. 25:21file. That's not exactly what we want.
  599. 25:24We want the PHP interpreter to execute
  600. 25:27instructions written from our file. In
  601. 25:29order to do so, a pair of PHP tags must
  602. 25:32be added to the file. Follow along with
  603. 25:35me. At the top of the file, add the
  604. 25:37following characters. less than question
  605. 25:40mark PHP space question mark greater
  606. 25:44than.
  607. 25:46Congrats, you've created your first pair
  608. 25:49of PHP tags. By adding these tags, we're
  609. 25:52entering PHP mode. Entering PHP mode
  610. 25:55allows our machines to begin executing
  611. 25:58instructions. These instructions can
  612. 26:00vary from sending an email, processing a
  613. 26:03transaction, or registering a new user
  614. 26:05into our system. PHP tags are always
  615. 26:09necessary. We must always tell the PHP
  616. 26:12interpreter which portion of the PHP
  617. 26:15file contains instructions and which
  618. 26:17portion contains regular HTML. As you
  619. 26:20might have already guessed, multiple PHP
  620. 26:23tags can be inserted into the file. For
  621. 26:26example, let's add another pair at the
  622. 26:29end of the file.
  623. 26:32Refreshing the page results in the same
  624. 26:34output as before. That's to be expected
  625. 26:37since we haven't written instructions
  626. 26:39from within the PHP tags. Before we do,
  627. 26:42there are a few more things worth
  628. 26:44mentioning. Firstly, PHP tags can be
  629. 26:48written from within HTML tags. As an
  630. 26:50example, let's replace the text inside
  631. 26:53the H1 tags with a pair of PHP tags.
  632. 26:58The text disappears from the preview.
  633. 27:01This is considered completely valid PHP
  634. 27:04code. Being able to write PHP inside
  635. 27:07HTML gives us the power to generate
  636. 27:10dynamic content in various locations of
  637. 27:13our HTML page. The second thing I wanted
  638. 27:16to mention was being unable to do the
  639. 27:18reverse. This time let's move the H1
  640. 27:21tags inside the PHP tags.
  641. 27:26A different result gets produced in the
  642. 27:28preview. An error gets rendered by the
  643. 27:31PHP interpreter. But why? This is
  644. 27:33because the rules of PHP mode are
  645. 27:36different from HTML mode.
  646. 27:40Inside PHP tags we must write valid
  647. 27:43code. In the English language, there are
  648. 27:45rules for how English is read and
  649. 27:47written. There's a specific structure we
  650. 27:50must follow for writing English, such as
  651. 27:52ending a sentence with a period or
  652. 27:54capitalizing the first word of a
  653. 27:56sentence. The rules for human languages
  654. 27:59are referred to as grammar. In a similar
  655. 28:02sense, programming languages have rules
  656. 28:04for how they're read and written. These
  657. 28:07rules are referred to as a programming
  658. 28:09languages syntax. This is why we're
  659. 28:11getting an error. We're not following
  660. 28:13PHP's syntax rules. As a result, the PHP
  661. 28:17interpreter is unhappy. In the next
  662. 28:19lecture, let's start learning PHP's
  663. 28:22syntax rules.
  664. 28:24[Music]
  665. 28:35In this lecture, we're going to start
  666. 28:37writing PHP code by learning about the
  667. 28:39echo keyword. First off, what is a
  668. 28:42keyword? Keywords are a feature in PHP
  669. 28:44for performing specific actions. If we
  670. 28:47type a keyword, the PHP interpreter
  671. 28:50understands that we're trying to perform
  672. 28:52a specific action based on that keyword.
  673. 28:55PHP offers various keywords. In the
  674. 28:58resource section of this lecture, I
  675. 29:00provide a link to an official list of
  676. 29:02keywords from the PHP documentation.
  677. 29:05Throughout this course, we're going to
  678. 29:07explore most of these keywords. Starting
  679. 29:09with the echo keyword. Let's head over
  680. 29:12to our editors. Previously, we added a
  681. 29:15few PHP tags. I think this is a bit
  682. 29:17excessive. Let's remove all of the tags
  683. 29:20except the opening tag.
  684. 29:24But why are we leaving just the opening
  685. 29:26tag? Don't we need a closing tag, too?
  686. 29:29Here's the cool thing about PHP. If we
  687. 29:31don't plan on exiting PHP mode, we don't
  688. 29:34need to add a closing PHP tag. A closing
  689. 29:37PHP tag becomes optional when we only
  690. 29:40intend to write PHP code. In most cases,
  691. 29:44developers often omit the closing PHP
  692. 29:46tags for files that only contain PHP
  693. 29:49code for readability. After removing the
  694. 29:52tags, let's write the echo keyword.
  695. 29:57So, what does the echo keyword do? The
  696. 30:00echo keyword instructs the interpreter
  697. 30:02to output content. Developers like using
  698. 30:04this keyword to output text. For
  699. 30:07example, let's add the following. Space
  700. 30:09quote hello world quote semicolon.
  701. 30:15Pay close attention to the types of
  702. 30:17quotes I'm using. I writing the message
  703. 30:19with double quotes, not single quotes.
  704. 30:22After adding this text, we can refresh
  705. 30:24the page to view the output. The message
  706. 30:26written after the echo keyword appears
  707. 30:29on the page. Pretty cool. But what's up
  708. 30:31with the strange syntax after the echo
  709. 30:34keyword? Let's break it down. Firstly,
  710. 30:36our message must be wrapped with quotes.
  711. 30:39This is how PHP is able to understand
  712. 30:42that we're trying to write raw text. Do
  713. 30:44you remember the last time we wrote HTML
  714. 30:47inside our PHP tags? PHP didn't like
  715. 30:50that. Previously, we had to adhere to
  716. 30:52the syntax rules of PHP. By using
  717. 30:55quotes, we're allowed to write whatever
  718. 30:57we want without PHP complaining. In this
  719. 31:01case, we're writing hello world. Lastly,
  720. 31:03we're adding a semicolon character. This
  721. 31:06character denotes the end of a
  722. 31:08statement.
  723. 31:11In my opinion, one of the hardest
  724. 31:13aspects of programming is learning the
  725. 31:15terminology. It's half the battle. A
  726. 31:18statement is another way of describing a
  727. 31:20single instruction given to a machine.
  728. 31:22In most cases, statements can be a
  729. 31:24single line of code. Programming
  730. 31:26languages give us the ability to
  731. 31:28communicate with a machine. Instructions
  732. 31:31can be given to a machine to perform
  733. 31:33various tasks from sending an email to
  734. 31:36processing a transaction. You can think
  735. 31:38of a statement as a single sentence in
  736. 31:41the English language. Multiple sentences
  737. 31:43can be combined to create a book. In a
  738. 31:46similar sense, multiple statements can
  739. 31:48be combined to create an application.
  740. 31:53So what's the difference between a
  741. 31:55keyword and a statement? A keyword
  742. 31:58refers to a single word in a line of
  743. 32:00code, whereas a statement describes the
  744. 32:03entire line of code. Back to the task at
  745. 32:06hand. We added a semicolon character to
  746. 32:09tell the PHP interpreter that we're
  747. 32:11finished with this line of code. PHP
  748. 32:13doesn't know when you're finished with a
  749. 32:15specific instruction. You must
  750. 32:17explicitly inform PHP when you're
  751. 32:19finished with an instruction. Once
  752. 32:21you've added the semicolon character,
  753. 32:23PHP moves on to the next instruction.
  754. 32:26So, for example, let's write another
  755. 32:28echo statement. This time, I'm going to
  756. 32:31write the following message. My name is
  757. 32:33John.
  758. 32:35In the preview, our new message gets
  759. 32:38rendered. You may have noticed, but PHP
  760. 32:40executes our code in the order the
  761. 32:42instructions are written. Statements are
  762. 32:45executed from top to bottom, similar to
  763. 32:47how books are read from top to bottom.
  764. 32:49The order of your code does matter.
  765. 32:54All right, I think it's time for an
  766. 32:55exercise. This exercise is fairly
  767. 32:58straightforward. I want you to wrap the
  768. 33:00echo statements we've written with a
  769. 33:02pair of H1 tags. That's it. Pause the
  770. 33:05video and give the exercise a try. Good
  771. 33:08luck.
  772. 33:11Welcome back. Hopefully, you were able
  773. 33:13to tackle the exercise. If not, that's
  774. 33:16fine. Let's walk through the solution
  775. 33:18together. First, let's close the PHP tag
  776. 33:21since we're going to write HTML.
  777. 33:26Next, wrap the PHP tags with a pair of
  778. 33:28H1 tags.
  779. 33:34That's it. The preview shows us that the
  780. 33:37text has been wrapped with H1 tags
  781. 33:39because the text is enlarged. Now, you
  782. 33:42may have come up with an alternative
  783. 33:44solution which is just as valid. An
  784. 33:46alternative solution would have been to
  785. 33:48write the H1 tags from within the
  786. 33:50quotes. I'll quickly undo the changes I
  787. 33:53made to my file.
  788. 33:56Next, I'll wrap the hello world message
  789. 33:59with H1 tags. These tags should be
  790. 34:02inserted from within the quotes, not
  791. 34:03outside.
  792. 34:06This time only the first message gets
  793. 34:09enlarged. So which solution is better?
  794. 34:12In our case, it doesn't matter. You're
  795. 34:14going to see both solutions in the real
  796. 34:16world. It's not uncommon to write PHP
  797. 34:19inside HTML tags or to write HTML tags
  798. 34:23from within an echo statement. Either
  799. 34:25solution is valid. Let's continue our
  800. 34:28journey in the next lecture. I'll see
  801. 34:30you there.
  802. 34:31[Music]
  803. 34:41In this lecture, we're going to learn
  804. 34:43another feature of PHP, which are
  805. 34:45comments. As you already know, comments
  806. 34:48in HTML were designed for adding notes
  807. 34:50in our code. They serve the same purpose
  808. 34:53in PHP. The main difference is how
  809. 34:55they're written. PHP supports three
  810. 34:58variations for comments. The first
  811. 35:01syntax for comments is two forward slash
  812. 35:03characters. After these characters, we
  813. 35:06can write whatever we want. We can even
  814. 35:08type gibberish. Comments are not
  815. 35:10processed by the PHP interpreter.
  816. 35:12Therefore, anything can be written
  817. 35:14inside a comment. Let's add a comment
  818. 35:17above the first echo statement.
  819. 35:21The comment we've written is considered
  820. 35:23a single line comment. As the name
  821. 35:25implies, only the code on this line is
  822. 35:27affected by the comment. If I were to
  823. 35:29attempt to write more text on a new
  824. 35:32line, errors would get thrown. In some
  825. 35:34cases, you may want to write a long
  826. 35:36comment. Technically, we could write a
  827. 35:38comment on a single line, but that would
  828. 35:40be harder to read. This dilemma leads us
  829. 35:42to the following type of comment called
  830. 35:44a multi-line comment. They're written
  831. 35:47with the following characters. slash
  832. 35:49asterisk asterisk slash.
  833. 35:54In between these characters, we're
  834. 35:56allowed to break our comments into
  835. 35:57multiple lines. As long as our text
  836. 36:00falls between these characters, they're
  837. 36:02considered to be within the bounds of
  838. 36:03the comment. This type of comment is
  839. 36:06called a multi-line comment. They were
  840. 36:08designed for longer comments. There's
  841. 36:10one more syntax I want to show you.
  842. 36:12There's an alternative syntax for
  843. 36:14writing a singleline comment. Rather
  844. 36:17than using forward slash characters, you
  845. 36:19can use a hash character.
  846. 36:23This character also denotes a single
  847. 36:25line comment. It's not as popular as
  848. 36:27other solutions, but you may come across
  849. 36:29it in a preview. Nothing appears aside
  850. 36:31from the messages in the echo
  851. 36:33statements. Comments are completely
  852. 36:35ignored by the PHP interpreter. Overall,
  853. 36:38comments are a feature for documenting
  854. 36:40our application. Throughout the course,
  855. 36:42I'll be using comments to add additional
  856. 36:44notes to our code. In the next lecture,
  857. 36:46let's start talking about variables.
  858. 36:49[Music]
  859. 36:59In this lecture, we're going to start
  860. 37:01using variables for storing data. So
  861. 37:04far, we've written comments. They're
  862. 37:06nice and all, but they're not all PHP
  863. 37:08has to offer. Programmers are often
  864. 37:10dealing with data. Data can range from
  865. 37:13addresses to the price of a product.
  866. 37:15Often, most pages on the web rely on
  867. 37:17data. Otherwise, we'd be staring at the
  868. 37:20same page. PHP has a feature called
  869. 37:22variables for storing data. A common
  870. 37:25analogy for variables is boxes. We can
  871. 37:28use boxes for storing physical items. If
  872. 37:31you're moving, it's common to label
  873. 37:32boxes to describe the contents of the
  874. 37:34box. For instance, if a box contains
  875. 37:37dishes and utensils, you may label the
  876. 37:40box as kitchen. The inner contents of
  877. 37:42the box can be considered the value.
  878. 37:44Variables are similar to boxes from the
  879. 37:47real world. Unlike boxes, variables
  880. 37:49don't store physical items. Instead,
  881. 37:52they can store file data, form data, or
  882. 37:55data from a database. They're extremely
  883. 37:57flexible for storing any kind of data.
  884. 38:01Let's try creating a variable in the
  885. 38:04index file. I'm going to clear the
  886. 38:06contents of the file except for the
  887. 38:08opening PHP tag. Everything else isn't
  888. 38:11necessary. After doing so, type the
  889. 38:13following dollar sign age semicolon.
  890. 38:18There are a few rules for writing
  891. 38:20variables in PHP. Variables must begin
  892. 38:23with a dollar sign character. By typing
  893. 38:26this character, PHP allocates memory on
  894. 38:28a user's machine to store data. This
  895. 38:31symbol is followed by the name of the
  896. 38:33variable.
  897. 38:35Variable names follow strict rules.
  898. 38:37Firstly, names are allowed to contain
  899. 38:40alphabetic characters, numbers, and
  900. 38:42underscores. Secondly, special
  901. 38:44characters are not allowed. For example,
  902. 38:47the at symbol can't be used as a
  903. 38:49variable name. Lastly, the first
  904. 38:51character of a variable name may be a
  905. 38:54letter or underscore. While numbers are
  906. 38:56allowed in variable names, they cannot
  907. 38:58be the first character. Besides these
  908. 39:01rules, PHP gives us a lot of freedom in
  909. 39:04naming our variables. There are a few
  910. 39:06good practices worth mentioning.
  911. 39:08Firstly, variable names should be
  912. 39:10descriptive. For example, let's say we
  913. 39:13had a variable for storing the total
  914. 39:15price of a user's purchase. The name of
  915. 39:17the variable could be called total
  916. 39:20price. That name is short, clear, and
  917. 39:22concise. A bad example would be an
  918. 39:24abbreviation called TP. We might
  919. 39:27understand what that means, but what
  920. 39:29would our co-workers think of the name?
  921. 39:31If you're working on a team, your team
  922. 39:33members may not be able to understand
  923. 39:35the purpose of the variable. Descriptive
  924. 39:37names provide clarity. In some cases,
  925. 39:41you may need to use abbreviations for
  926. 39:43variables with long names. If that's the
  927. 39:45case, your abbreviations should be
  928. 39:48easily recognizable. For example, we may
  929. 39:51need to store the name of a customer. A
  930. 39:53good abbreviation would be cust name
  931. 39:56where cust is short for customer.
  932. 40:00However, using the name cust n is harder
  933. 40:03to recognize. Another useful tip is to
  934. 40:06avoid using single letters as variable
  935. 40:09names. This circles back to the first
  936. 40:11tip. Using a single letter doesn't
  937. 40:13describe the type of data stored in a
  938. 40:15variable. With that being said, some
  939. 40:17developers will use a single letter when
  940. 40:20dealing with loops. That's the only
  941. 40:22scenario where using single letters is
  942. 40:24fine. In most cases, you should stick to
  943. 40:27using longer descriptive names. Being
  944. 40:30consistent is another good habit to pick
  945. 40:32up. Your variable names should have
  946. 40:34consistent spelling. Mixing the casing
  947. 40:37of your variables can result in
  948. 40:39confusing and unreadable code. Last but
  949. 40:42not least, you should avoid generic
  950. 40:44names. For example, you may need to
  951. 40:46store user data in a variable. It may be
  952. 40:49tempting to call the variable data, but
  953. 40:51that doesn't exactly describe what type
  954. 40:53of data you're storing. Are you storing
  955. 40:56file data, database data, or maybe
  956. 40:58you're storing product data? The more
  957. 41:00precise you are, the better. Overall,
  958. 41:03these tips should help you with naming
  959. 41:05your variables. What if you need to
  960. 41:07write multiple words from within a
  961. 41:09variable name? As mentioned before, we
  962. 41:11can't use special characters such as
  963. 41:13spaces. Developers have come up with
  964. 41:16three naming conventions for these
  965. 41:18scenarios. The first naming convention
  966. 41:20is called camelcasing. In this format,
  967. 41:23every word is capitalized except for the
  968. 41:25first word. By doing so, it's easier to
  969. 41:28read the words in a variable name. Up
  970. 41:30next, we have a format called Pascal
  971. 41:33Casing, which is similar to camel
  972. 41:34casing. Every word is capitalized,
  973. 41:37including the first word. Lastly, snake
  974. 41:40casing is another format that uses
  975. 41:42lowercase letters. Instead of
  976. 41:44capitalizing each word, words are
  977. 41:46separated with underscores. You're
  978. 41:48allowed to use whatever naming
  979. 41:50convention suits your application.
  980. 41:52Generally speaking, most PHP developers
  981. 41:54prefer camel casing for variables.
  982. 41:57However, if you decide to use a
  983. 41:59different naming convention, that's
  984. 42:01perfectly fine. The most important thing
  985. 42:03is to be consistent. For this course,
  986. 42:06I'll be sticking with camel casing for
  987. 42:08multi-worded variable names. We've
  988. 42:10successfully created our first variable.
  989. 42:13Currently, it doesn't store anything. In
  990. 42:15the next set of lectures, let's start
  991. 42:17storing data.
  992. 42:19[Music]
  993. 42:30In this lecture, we're going to store a
  994. 42:32value inside the variable we created
  995. 42:34from the previous lecture. We can
  996. 42:36accomplish this task by using an
  997. 42:38operator. To be more specific, the
  998. 42:40assignment operator. But what is an
  999. 42:42operator? An operator is a symbol or
  1000. 42:45character that accepts a value. This
  1001. 42:48value then gets produced into an
  1002. 42:50entirely new value. This is important to
  1003. 42:53understand. Operators always create a
  1004. 42:56brand new value from an existing value.
  1005. 43:00In the resource section of this lecture,
  1006. 43:02I provide a link to PHP's documentation
  1007. 43:05page for its operators. Out of the box,
  1008. 43:08dozens of operators are available for
  1009. 43:10various actions. We're going to be
  1010. 43:12exploring most of them throughout the
  1011. 43:14course. To get started, let's take a
  1012. 43:16look at the assignment operator. The job
  1013. 43:19of the assignment operator is to store a
  1014. 43:22value in a variable. Back in our
  1015. 43:24project, we have a variable called age,
  1016. 43:26which doesn't store anything. Let's
  1017. 43:28update the variable by typing the
  1018. 43:30following after the name of the
  1019. 43:32variable. space equals space 29.
  1020. 43:37In this example, we're storing a number.
  1021. 43:39Unlike raw text, we don't have to wrap
  1022. 43:42numbers with quotes. They're optional
  1023. 43:44whenever we're dealing with numbers.
  1024. 43:46Next, let's try echoing the age.
  1025. 43:50In the preview, the age gets displayed.
  1026. 43:53Perfect. We've got a variable for
  1027. 43:55storing the age. But how is it working?
  1028. 43:57The equal sign character is known as the
  1029. 44:00assignment operator. This operator can
  1030. 44:02be used to store a value inside a
  1031. 44:05variable. The variable name must always
  1032. 44:07appear to the left of the operator. The
  1033. 44:10value must appear to the right. Creating
  1034. 44:13variables allows us to reference a value
  1035. 44:15anywhere in our script. In our case,
  1036. 44:18we're referencing the value from the
  1037. 44:19echo statement. Previously, we would
  1038. 44:22type the value immediately after the
  1039. 44:24echo keyword. This time, we're
  1040. 44:27referencing the variable by typing its
  1041. 44:29name. Whenever variables are created,
  1042. 44:32PHP stores the value in its memory.
  1043. 44:34Afterward, if PHP encounters a reference
  1044. 44:38to a variable, it'll replace the
  1045. 44:40reference with the value stored within
  1046. 44:42the variable. In a nutshell, typing echo
  1047. 44:45age is the same as typing echo 29.
  1048. 44:50Hopefully, you can see the benefits of
  1049. 44:52variables. Storing values in variables
  1050. 44:55is more efficient as we can reference
  1051. 44:57the value multiple times anywhere in our
  1052. 45:00script. Things get even more interesting
  1053. 45:02when trying to update a variable. For
  1054. 45:05example, let's say my birthday pops up.
  1055. 45:07This means my age should change.
  1056. 45:10Variables can be updated the same way
  1057. 45:12they're created. On a new line, type the
  1058. 45:14following dollar sign age equals 30
  1059. 45:18semicolon.
  1060. 45:21PHP is smart enough to update the
  1061. 45:23existing variable to the new value. We
  1062. 45:25don't have to worry about our programs
  1063. 45:27hogging resources whenever a variable
  1064. 45:29needs to be updated. Isn't that awesome?
  1065. 45:32PHP is capable of being efficient with
  1066. 45:34our programs variables. The number 30
  1067. 45:37appears in the preview, thus proving
  1068. 45:39that our variable was updated. Before I
  1069. 45:42end this lecture, there's one warning I
  1070. 45:44want to put out there. As we know,
  1071. 45:46variables are case-sensitive. It can be
  1072. 45:49easy to create a variable when you
  1073. 45:51originally intended to update a
  1074. 45:53variable. For example, let's say I typed
  1075. 45:56the word age with a capital A.
  1076. 46:01In our preview, the original number gets
  1077. 46:03rendered. Despite being spelled the
  1078. 46:06same, PHP interprets these definitions
  1079. 46:08as completely different variables. A
  1080. 46:11single typo like this can cause issues.
  1081. 46:14Worst of all, PHP does not throw errors
  1082. 46:16for simple variable typos. If you ever
  1083. 46:20run into a problem where your program is
  1084. 46:22not behaving as expected, the problem
  1085. 46:24may have to do with a typo. Typos are
  1086. 46:26more of a common problem than you think.
  1087. 46:29So, just be aware of that when working
  1088. 46:31with variables. I'm going to remove the
  1089. 46:33second age variable. We don't need it
  1090. 46:36anymore. After removing that variable,
  1091. 46:38let's get into data types.
  1092. 46:41[Music]
  1093. 46:51In this lecture, we're going to briefly
  1094. 46:53discuss data types. Applications can
  1095. 46:56store various types of data. Variables
  1096. 46:58are the most common solution for storing
  1097. 47:01data. For the best performance possible,
  1098. 47:03programming languages categorize data.
  1099. 47:06Categories for data are known as data
  1100. 47:08types. For example, if you're working
  1101. 47:10with numbers, the data type will be an
  1102. 47:13integer. If you're working with a file,
  1103. 47:15the data type should be for a file. So
  1104. 47:17on and so forth. But why why is it
  1105. 47:20important to categorize our data?
  1106. 47:23Categorizing data allows programming
  1107. 47:25languages to optimize your applications.
  1108. 47:27If a variable stores a number, the
  1109. 47:29program never has to worry about working
  1110. 47:32with dates or random text. In addition,
  1111. 47:35it provides clarity as to the type of
  1112. 47:37data you're working with, which can be
  1113. 47:39helpful with debugging an application.
  1114. 47:42Data types are important in PHP because
  1115. 47:45they determine the type of data that a
  1116. 47:47variable can store and how it can be
  1117. 47:49used in your code. By specifying the
  1118. 47:52data type of a variable, you can ensure
  1119. 47:54that it can only be used in a certain
  1120. 47:57way and prevent unexpected errors from
  1121. 47:59occurring when the code is executed. For
  1122. 48:02example, consider a scenario where you
  1123. 48:04need to calculate the average of a set
  1124. 48:06of numbers. If you store these numbers
  1125. 48:08with the incorrect data type, you will
  1126. 48:11not be able to perform mathematical
  1127. 48:13operations on them. Any calculations
  1128. 48:15would produce inaccurate results.
  1129. 48:17However, if you store these numbers
  1130. 48:19correctly, you can perform the
  1131. 48:21calculation as expected. Here's an
  1132. 48:24analogy to help understand the
  1133. 48:26importance of data types. Imagine you
  1134. 48:28are cooking a meal and need to measure
  1135. 48:30ingredients. If you were to use a cup to
  1136. 48:33measure salt instead of a teaspoon, the
  1137. 48:35amount you would use would be
  1138. 48:37significantly different and the meal
  1139. 48:39would not turn out as expected. This is
  1140. 48:41similar to how using the wrong data type
  1141. 48:43in your code can result in unexpected
  1142. 48:45results. By specifying the correct data
  1143. 48:48type, you can ensure that your code
  1144. 48:50produces the correct results, thus
  1145. 48:52preventing bugs and errors from
  1146. 48:54occurring. With that being said,
  1147. 48:56programming languages tackle data types
  1148. 48:58differently. The concept of a data type
  1149. 49:01is not unique to PHP. Most modern
  1150. 49:04programming languages have data types.
  1151. 49:06Generally speaking, there are two types
  1152. 49:08of languages, which are statically typed
  1153. 49:11languages and dynamically typed
  1154. 49:13languages. Statically typed languages
  1155. 49:16require developers to be explicit with
  1156. 49:18their data types. A variable cannot be
  1157. 49:21declared without a type. After setting
  1158. 49:23the type, the variable's type may never
  1159. 49:25change for the lifetime of the program.
  1160. 49:28In contrast, dynamically typed languages
  1161. 49:31do not require developers to set the
  1162. 49:33type. The programming language can
  1163. 49:36automatically determine the type for
  1164. 49:38you. Flexibility is another component of
  1165. 49:40dynamically typed languages. We're
  1166. 49:43allowed to change the type at any time.
  1167. 49:46For example, initially we may want to
  1168. 49:48store a number in a variable. In the
  1169. 49:51future, we have the option of storing a
  1170. 49:53file in the same variable. This action
  1171. 49:56will be acceptable under a dynamically
  1172. 49:58typed language. So where does PHP land?
  1173. 50:02PHP is a dynamically typed language.
  1174. 50:05Dynamically typed languages are
  1175. 50:07considered to be beginnerfriendly. It's
  1176. 50:09one less step you must take to write an
  1177. 50:11application. Unfortunately, there are
  1178. 50:13downsides to dynamically typed
  1179. 50:15languages. First and foremost, they can
  1180. 50:18be slightly slower than statically typed
  1181. 50:21languages. By shifting the
  1182. 50:22responsibility of assigning a type to
  1183. 50:24the programming language, languages must
  1184. 50:27work harder to categorize your data.
  1185. 50:29Secondly, you're not guaranteed a
  1186. 50:31variable may or may the same type for
  1187. 50:33the lifetime of a program. Suddenly, a
  1188. 50:36variable can change from being a number
  1189. 50:38to a string. Inconsistencies can cause a
  1190. 50:41program to behave unexpectedly.
  1191. 50:43Fortunately, PHP has grown to address
  1192. 50:46the shortcomings of a dynamically typed
  1193. 50:48language. As we progress through the
  1194. 50:50course, we'll come across proper
  1195. 50:52debugging techniques and best practices
  1196. 50:55to avoid errors. First, let's start by
  1197. 50:58exploring the data types available from
  1198. 51:00PHP. In total, PHP has nine data types.
  1199. 51:04They're called null, boolean, integer,
  1200. 51:07float, string, array, object, callable,
  1201. 51:11and resource. We're not going to be able
  1202. 51:14to explore all data types as some of
  1203. 51:16them are for advanced use cases. Let's
  1204. 51:19focus on the simpler data types. In the
  1205. 51:22next set of lectures, let's try viewing
  1206. 51:24the data type of a variable.
  1207. 51:26[Music]
  1208. 51:36In this lecture, we're going to start
  1209. 51:38using functions. Functions are another
  1210. 51:41commonly used feature of PHP. So what is
  1211. 51:43a function? Functions are a feature for
  1212. 51:46performing a specific set of
  1213. 51:48instructions. Hold on a moment. Isn't
  1214. 51:50that the same as keywords? As we know,
  1215. 51:53keywords are also used for giving the
  1216. 51:55machine instructions to perform. So what
  1217. 51:58are the differences? There are actually
  1218. 52:00a lot of differences, but for the sake
  1219. 52:03of simplicity, I'm going to summarize it
  1220. 52:05into one key difference. Keywords are
  1221. 52:08considered to be a core feature of the
  1222. 52:09PHP language. We can't disable or remove
  1223. 52:12keywords from PHP. On the other hand,
  1224. 52:15functions can be disabled from the
  1225. 52:17language. But why would someone want to
  1226. 52:19do that? PHP has a few questionable
  1227. 52:22functions in its repertoire. There are
  1228. 52:24developers who believe these functions
  1229. 52:26should be cautiously used by the
  1230. 52:28community. To prevent developers from
  1231. 52:30accidentally using a dangerous function,
  1232. 52:32it's advisable to disable them.
  1233. 52:36Keywords can't be disabled, whereas
  1234. 52:38functions can be disabled. We're not
  1235. 52:40going to need to disable any function
  1236. 52:42for this course. However, it is
  1237. 52:44something you should be aware of when
  1238. 52:46dealing with functions. In the resource
  1239. 52:48section of this lecture, I provide a
  1240. 52:51link to an official list of functions
  1241. 52:53available from PHP. Almost immediately,
  1242. 52:56you're going to notice another
  1243. 52:57difference between keywords and
  1244. 52:59functions. PHP has hundreds of
  1245. 53:01functions, whereas there are only a
  1246. 53:04handful of keywords. Scrolling through
  1247. 53:06this page, you'll find a function for
  1248. 53:08just about anything. You can perform
  1249. 53:10actions from reading a file to
  1250. 53:12encrypting data. I know it can feel
  1251. 53:15overwhelming at first, but don't feel
  1252. 53:17stressed about the enormous list of
  1253. 53:19functions. It's highly unlikely you're
  1254. 53:22ever going to need all of them. I myself
  1255. 53:24haven't even used more than 20% of the
  1256. 53:27functions listed on this page. So, what
  1257. 53:30functions are we going to use? For
  1258. 53:32starters, we're going to use a function
  1259. 53:34called variable dump. Let's head over to
  1260. 53:36our project. Inside the script, I'm
  1261. 53:39going to replace the existing code with
  1262. 53:41a function called variable dump.
  1263. 53:46Functions are easy to write. A function
  1264. 53:48can be executed by typing the name of
  1265. 53:50the function followed by a pair of
  1266. 53:52parenthesis. But what are the
  1267. 53:54parentheses for, you may ask? Well, we
  1268. 53:57can pass on data to the function. The
  1269. 53:59question is what kind of data? That'll
  1270. 54:02vary from function to function. The job
  1271. 54:05of the variable dump function is to
  1272. 54:07output a value onto the screen. It's
  1273. 54:10very similar to the echo keyword.
  1274. 54:12However, there's one critical
  1275. 54:13difference. The variable dump function
  1276. 54:16also outputs the data type of the value.
  1277. 54:19For example, let's type 29 into the
  1278. 54:22parenthesis.
  1279. 54:25This time the page displays a value but
  1280. 54:28the data type as well. The variable dump
  1281. 54:30function was introduced to help
  1282. 54:32developers debug their programs. It can
  1283. 54:35be useful to view the data type of your
  1284. 54:37values. In the next set of lectures,
  1285. 54:40we're going to start using this function
  1286. 54:42to help us inspect the various data
  1287. 54:44types offered by the PHP language.
  1288. 54:48[Music]
  1289. 54:58In this lecture, we're going to explore
  1290. 55:00our first data type called null. The
  1291. 55:03null data type represents nothing. If
  1292. 55:05your variable does not store a value,
  1293. 55:08it's considered to be null. Seems
  1294. 55:10strange, but it's not uncommon to have
  1295. 55:12variables that store nothing. In the
  1296. 55:14real world, you may have a mailbox for
  1297. 55:16storing your mail. However, your mailbox
  1298. 55:19is not always full. From time to time,
  1299. 55:21it'll be empty. Just because it's empty
  1300. 55:24doesn't mean you should get rid of your
  1301. 55:25mailbox. Eventually, you're going to
  1302. 55:27receive mail. This same idea can apply
  1303. 55:30to programming. For example, you may
  1304. 55:33want to create a login form for users.
  1305. 55:35It's common for login forms to require a
  1306. 55:38user's email. This information would be
  1307. 55:40stored in a variable. Initially, the
  1308. 55:42form may not be filled in. Therefore,
  1309. 55:45you may have a variable in your
  1310. 55:46application without a value. Let's look
  1311. 55:49at an example by creating a variable
  1312. 55:51called data. I know what you're
  1313. 55:53thinking. Didn't I say earlier that our
  1314. 55:56variable names should be descriptive?
  1315. 55:58That's correct. But for this example,
  1316. 56:00the data variable is going to store
  1317. 56:02various types of data. It's going to be
  1318. 56:05a generic variable for experimenting
  1319. 56:07with data types. After creating the
  1320. 56:10variable, set the value to null.
  1321. 56:14And that's it. We've created a variable
  1322. 56:17that doesn't store anything. Let's
  1323. 56:19replace the number inside the variable
  1324. 56:22dump function with the data variable.
  1325. 56:26After doing so, the value gets rendered
  1326. 56:29on the screen. Now, you may be thinking,
  1327. 56:31why should we take the time to set the
  1328. 56:33variable to null? If it doesn't contain
  1329. 56:35a value, is there a point in defining
  1330. 56:38one? The answer to that question is yes.
  1331. 56:41Let me show you why. In our script,
  1332. 56:43apply a comment to the data variable.
  1333. 56:48There are two things worth noting about
  1334. 56:50the output. Firstly, PHP throws a
  1335. 56:53warning at us. The warning states the
  1336. 56:55following. Undefined variable data.
  1337. 56:58Undefined warnings are one of the most
  1338. 57:00common errors to receive in PHP. The
  1339. 57:02interpreter is trying to tell us that
  1340. 57:04we're trying to use a variable that
  1341. 57:06doesn't exist in our program. Makes
  1342. 57:08sense, right? PHP can't use a variable
  1343. 57:12that hasn't been created in the first
  1344. 57:14place. The second noteworthy thing is
  1345. 57:16the text after the warning. The value
  1346. 57:19null gets outputed by default. If PHP
  1347. 57:22can't find a variable, it'll fall back
  1348. 57:24to null. Undefined variables are
  1349. 57:27considered to be null. While it's great
  1350. 57:29that undefined variables have a default
  1351. 57:32value, the warning should be avoided. If
  1352. 57:34we were to explicitly create the
  1353. 57:36variable with an initial value of null,
  1354. 57:39we can avoid the warning altogether. I'm
  1355. 57:41going to uncomment the data variable.
  1356. 57:46There's one more thing I want to mention
  1357. 57:48about the null data type. The null
  1358. 57:50keyword is case insensitive. Most
  1359. 57:53developers prefer to use lowercase
  1360. 57:55letters. From time to time, you may come
  1361. 57:57across a different format that uses all
  1362. 58:00uppercase letters like so.
  1363. 58:04There isn't a difference between the
  1364. 58:05spellings. Both formatting solutions
  1365. 58:08result in the same thing. Personally, I
  1366. 58:10prefer to use lowercase letters. All
  1367. 58:13right, that's about it for the null data
  1368. 58:15type. In the next lecture, let's
  1369. 58:17continue exploring other data types.
  1370. 58:20[Music]
  1371. 58:30In this lecture, we're going to explore
  1372. 58:33another data type called boolean.
  1373. 58:35Booleans are a data type for storing
  1374. 58:37truthy or falsy values. That may seem
  1375. 58:40strange. Why would we want to use this
  1376. 58:42data type? It can be useful for
  1377. 58:44determining whether an action can be
  1378. 58:46performed. For example, let's say
  1379. 58:49visitors must be logged in to perform a
  1380. 58:51purchase. If a visitor is not logged in,
  1381. 58:54they will not be able to check out.
  1382. 58:56Storing the user's currently
  1383. 58:57authenticated status as a variable is a
  1384. 59:00common task. Using the boolean type for
  1385. 59:03this scenario would be perfect. In fact,
  1386. 59:05let's give that a try. In the script,
  1387. 59:08define a variable below the data
  1388. 59:10variable called is logged in.
  1389. 59:15Set the value to true.
  1390. 59:19Boolean values can either be true or
  1391. 59:22false. The names are case insensitive.
  1392. 59:25Sometimes developers will use all
  1393. 59:27uppercase letters. There isn't a
  1394. 59:29difference in the formatting. It all
  1395. 59:31boils down to personal preference. For
  1396. 59:33this course, I'll be using lowercase
  1397. 59:35letters. There's something worth
  1398. 59:37pointing out about the name. The name of
  1399. 59:39the variable is a question. For
  1400. 59:41variables that store boolean values,
  1401. 59:44it's common practice to format the name
  1402. 59:46of the variable in the form of a
  1403. 59:48question. For example, let's say we were
  1404. 59:50displaying a list of blog posts. If the
  1405. 59:53user has not written any blog posts, we
  1406. 59:56should display a message to inform them
  1407. 59:58of the absence of blog posts. Storing
  1408. 1:00:01this information as a variable would
  1409. 1:00:03make sense. In this scenario, the
  1410. 1:00:05variable could be called has blog posts.
  1411. 1:00:10Throughout the course, we'll try our
  1412. 1:00:12best to follow this practice. It's not
  1413. 1:00:15required, but recommended. You're going
  1414. 1:00:17to see it all the time in your
  1415. 1:00:18programming career. Let's update the
  1416. 1:00:21value passed into the variable dump
  1417. 1:00:23function to the is logged in variable.
  1418. 1:00:28In the preview, the data type is boolean
  1419. 1:00:32or bool for short. Wrapped inside
  1420. 1:00:34parentheses is the value. For
  1421. 1:00:36demonstration purposes, I'll update the
  1422. 1:00:39variable to false.
  1423. 1:00:42As you can see, the value has been
  1424. 1:00:44updated to reflect the new value.
  1425. 1:00:47Booleans are one of the most memory
  1426. 1:00:49efficient data types. They're perfect
  1427. 1:00:51for scenarios that require a yes or no
  1428. 1:00:53answer. In the next lecture, let's start
  1429. 1:00:56exploring data types for numbers.
  1430. 1:00:58[Music]
  1431. 1:01:09In this lecture, we're going to start
  1432. 1:01:11exploring two data types for numbers
  1433. 1:01:13called integers and floats. Initially,
  1434. 1:01:16that may seem strange. Why are there two
  1435. 1:01:19data types for numbers? The reason has
  1436. 1:01:21to do with memory efficiency. Storing
  1437. 1:01:24numbers with decimal values requires
  1438. 1:01:26additional work. They're not easy to
  1439. 1:01:28store. Therefore, it's efficient to have
  1440. 1:01:31a data type dedicated to whole numbers
  1441. 1:01:34and another data type for numbers with
  1442. 1:01:36decimal values. Most of the complexities
  1443. 1:01:39are handled for you. It's not something
  1444. 1:01:41you'll have to worry about. Regardless,
  1445. 1:01:43it can be beneficial to know the
  1446. 1:01:45differences. Let's start with integers.
  1447. 1:01:48The integer data type was designed for
  1448. 1:01:50storing whole numbers in our script.
  1449. 1:01:53Let's set the data variable to a random
  1450. 1:01:56whole number.
  1451. 1:01:59Next, let's update the variable dump
  1452. 1:02:02function to output the data variable.
  1453. 1:02:06As you can see, the data type has been
  1454. 1:02:09set to integer. PHP supports negative
  1455. 1:02:12numbers, too. For example, let's add the
  1456. 1:02:15negative symbol before the number.
  1457. 1:02:19Despite adding this symbol, the value
  1458. 1:02:22continues to be an integer. What if
  1459. 1:02:24we're dealing with large numbers? For
  1460. 1:02:26example, I'm going to set the value to 5
  1461. 1:02:28million.
  1462. 1:02:31If you're like me, you may have
  1463. 1:02:33difficulty reading this number.
  1464. 1:02:34Unfortunately, PHP does not support
  1465. 1:02:37commas in our numbers. Instead of using
  1466. 1:02:40commas, we can use underscore
  1467. 1:02:42characters. Underscore characters can be
  1468. 1:02:44inserted anywhere inside the number.
  1469. 1:02:46It's common practice to insert these
  1470. 1:02:48characters for every three numbers. I'm
  1471. 1:02:51going to quickly add them in.
  1472. 1:02:55These characters don't affect the
  1473. 1:02:56number. PHP removes them during runtime.
  1474. 1:03:00The purpose of these characters was to
  1475. 1:03:02help with readability for large numbers.
  1476. 1:03:04They are completely optional. If you can
  1477. 1:03:06read large numbers without these
  1478. 1:03:08characters, don't feel pressured to add
  1479. 1:03:10them. After updating the script, the
  1480. 1:03:12large number appears normally in the
  1481. 1:03:14preview. PHP strips away the underscore
  1482. 1:03:18characters from the numbers. we never
  1483. 1:03:20have to worry about them affecting our
  1484. 1:03:22program. Let's shift our focus to
  1485. 1:03:25floats. A float is the data type for
  1486. 1:03:28numbers with decimals. Similar to
  1487. 1:03:30integers, floats support negative
  1488. 1:03:32numbers and underscore characters for
  1489. 1:03:35large numbers. Let's create our first
  1490. 1:03:37float. Set the data variable to the
  1491. 1:03:40following value 1 2 3.45.
  1492. 1:03:46You may be wondering why is it called a
  1493. 1:03:48float? The reason has to do with the
  1494. 1:03:50placement of the decimal separator. The
  1495. 1:03:53dot can be positioned anywhere in the
  1496. 1:03:55number. We can position it at the
  1497. 1:03:57beginning of the number. Alternatively,
  1498. 1:03:59we can position it between the second
  1499. 1:04:01and third numbers. You can think of it
  1500. 1:04:03as a character that can float anywhere
  1501. 1:04:06around the entirety of the number. If
  1502. 1:04:08you think that's strange, I would agree
  1503. 1:04:11with you. There's a joke among the
  1504. 1:04:12community that programmers are terrible
  1505. 1:04:14at naming things. It would have been
  1506. 1:04:16simpler to use the word decimal as the
  1507. 1:04:19name for the type. Regardless, it's been
  1508. 1:04:21established that numbers with decimal
  1509. 1:04:24values are considered to be floats. In
  1510. 1:04:26the preview, the data type has been
  1511. 1:04:28marked as a float. Perfect. Integers and
  1512. 1:04:31floats are two data types for storing
  1513. 1:04:34numbers. However, PHP is doing us a
  1514. 1:04:36favor by optimizing the memory for our
  1515. 1:04:39program. Numbers with decimals require
  1516. 1:04:42more memory than numbers without decimal
  1517. 1:04:44values. Overall, we benefit from this
  1518. 1:04:47behavior. In the next lecture, let's
  1519. 1:04:49continue exploring data types.
  1520. 1:04:52[Music]
  1521. 1:05:02In this lecture, we're going to explore
  1522. 1:05:05another data type called strings.
  1523. 1:05:07Strings are the data type for storing
  1524. 1:05:09text. Sounds like a strange name. Why
  1525. 1:05:12would developers call it a string? Text
  1526. 1:05:14is a combination of multiple characters
  1527. 1:05:16that are strung together, hence the name
  1528. 1:05:19string. It's a weird naming convention,
  1529. 1:05:21but that's what the community has
  1530. 1:05:23settled on. Regardless, if you're
  1531. 1:05:25working with text, the data type is
  1532. 1:05:27considered to be a string. Let's try
  1533. 1:05:30creating a string. For the data
  1534. 1:05:32variable, let's replace the existing
  1535. 1:05:34value with the following value. John
  1536. 1:05:39text must be wrapped with quotes. Single
  1537. 1:05:42quotes and double quotes are completely
  1538. 1:05:44valid. In this example, I'm using single
  1539. 1:05:47quotes. For demonstration purposes, I'll
  1540. 1:05:50use double quotes for a different
  1541. 1:05:52variable. Below, create another variable
  1542. 1:05:55called last name with a value of Smith.
  1543. 1:06:01This time I'm using double quotes.
  1544. 1:06:03Inside the quotes, we're allowed to
  1545. 1:06:05write anything we'd like. blog posts,
  1546. 1:06:08product names, addresses, or anything
  1547. 1:06:11represented as text can be stored in a
  1548. 1:06:13string. It's important to note that code
  1549. 1:06:16inside a string won't be executed. For
  1550. 1:06:19example, inside the data variables
  1551. 1:06:22value, let's call the variable dump
  1552. 1:06:24function.
  1553. 1:06:27As you can see, the contents of the
  1554. 1:06:29value do not get executed. PHP does not
  1555. 1:06:32run the variable dump function from
  1556. 1:06:34within the string. It's interpreted as
  1557. 1:06:37plain text. If we shift our focus to the
  1558. 1:06:39data type, it's been recognized as a
  1559. 1:06:42string. Looking closely at the output,
  1560. 1:06:45PHP has taken the time to count the
  1561. 1:06:47number of characters from within the
  1562. 1:06:49string. Overall, strings are simple to
  1563. 1:06:52create. As long as you're wrapping text
  1564. 1:06:54with quotes, you're good to go. The
  1565. 1:06:56question is, what's the difference
  1566. 1:06:58between single quotes and double quotes?
  1567. 1:07:01Most developers prefer single quotes for
  1568. 1:07:04readability. There's less clutter.
  1569. 1:07:06However, double quotes offer one
  1570. 1:07:08advantage over single quotes.
  1571. 1:07:12PHP supports string interpolation. I
  1572. 1:07:15know that looks like a scary,
  1573. 1:07:16complicated word, but I promise it's
  1574. 1:07:19not. String interpolation is the process
  1575. 1:07:21of replacing a placeholder with a value.
  1576. 1:07:24It's as simple as that. For example,
  1577. 1:07:27let's say we have the following
  1578. 1:07:28sentence. Hello, my name is X. The
  1579. 1:07:31letter X is a placeholder. We can swap
  1580. 1:07:34this placeholder with an actual value.
  1581. 1:07:37String interpolation is a common feature
  1582. 1:07:39you'll be using in your programs.
  1583. 1:07:43This feature is only available with
  1584. 1:07:45strings that are wrapped with double
  1585. 1:07:47quotes. Inside the last name variable,
  1586. 1:07:50let's inject the data variable by adding
  1587. 1:07:53it at the beginning of the string.
  1588. 1:07:57We're allowed to inject variables into a
  1589. 1:08:00string. PHP replaces this portion of the
  1590. 1:08:03string with the value stored in the data
  1591. 1:08:05variable. Before testing our work, let's
  1592. 1:08:08remove the variable dump function from
  1593. 1:08:11the data variable.
  1594. 1:08:14Next, update the variable dump function
  1595. 1:08:17to output the last name variable.
  1596. 1:08:22The entire name has been rendered in the
  1597. 1:08:24preview. String interpolation is a
  1598. 1:08:26simple yet handy feature. There's one
  1599. 1:08:29thing I should mention. In some cases,
  1600. 1:08:31you may want to append text immediately
  1601. 1:08:34after a variable. For example, in the
  1602. 1:08:37last name variable, I'll add the
  1603. 1:08:39trademark symbol after the data
  1604. 1:08:41variable.
  1605. 1:08:44Adding this symbol produces an error.
  1606. 1:08:46The reason is simple. PHP assumes that
  1607. 1:08:49the trademark symbol is part of the
  1608. 1:08:51variable name. It'll attempt to search
  1609. 1:08:54for a variable with this name, which
  1610. 1:08:56doesn't exist. So, how do we output the
  1611. 1:08:59variable while keeping the symbol? We
  1612. 1:09:01can wrap the variable with curly
  1613. 1:09:03brackets like so.
  1614. 1:09:07Curly brackets are optional. However, if
  1615. 1:09:09they're added, PHP ignores text written
  1616. 1:09:12outside of it. You don't have to worry
  1617. 1:09:15about the curly brackets appearing
  1618. 1:09:16within the string. PHP removes them
  1619. 1:09:19during output. In the output, the full
  1620. 1:09:21name gets outputed. Perfect. There's one
  1621. 1:09:24more thing I want to show you before
  1622. 1:09:26ending the lecture. So far, we've been
  1623. 1:09:29rendering the entire string. In some
  1624. 1:09:31cases, you may want to output a specific
  1625. 1:09:34character. It turns out that PHP allows
  1626. 1:09:37us to access specific characters from a
  1627. 1:09:39string. In the variable dump function,
  1628. 1:09:42let's add a pair of square brackets.
  1629. 1:09:46If we're dealing with a string, we can
  1630. 1:09:49access a specific character by adding
  1631. 1:09:51these brackets. Within these brackets,
  1632. 1:09:53we must add a number. Behind the scenes,
  1633. 1:09:56PHP indexes our string. It'll assign a
  1634. 1:09:59number to each character. The first
  1635. 1:10:01character is assigned an index of zero.
  1636. 1:10:04The second character is assigned an
  1637. 1:10:06index of one. So on and so forth. As an
  1638. 1:10:09exercise, what number do you think is
  1639. 1:10:11assigned to the fourth character? I'll
  1640. 1:10:14give you a moment to think about this.
  1641. 1:10:18The answer is three. Let's try testing
  1642. 1:10:21our theory. In the square brackets, type
  1643. 1:10:23the number three.
  1644. 1:10:27Cool. As you can see, the fourth
  1645. 1:10:29character gets outputed. It might seem
  1646. 1:10:31strange. Programming languages commonly
  1647. 1:10:34count at zero instead of one. It's a
  1648. 1:10:36strange habit you'll have to get used
  1649. 1:10:38to. Don't worry. As we progress through
  1650. 1:10:41the course, counting from zero will
  1651. 1:10:43become natural. We're not limited to
  1652. 1:10:45accessing a character. We can also
  1653. 1:10:48update a specific character from a
  1654. 1:10:50string. Above the variable dump
  1655. 1:10:52function, let's set the last name three
  1656. 1:10:55variable to X.
  1657. 1:10:59Once again, I'm using the square bracket
  1658. 1:11:02syntax. This time we're directly
  1659. 1:11:04updating this character. PHP won't
  1660. 1:11:07update the entire string. The fourth
  1661. 1:11:09character has been changed to X. There's
  1662. 1:11:12one thing worth highlighting. We're
  1663. 1:11:14updating the character before running
  1664. 1:11:16the function. The order of our code does
  1665. 1:11:19matter. PHP executes code from top to
  1666. 1:11:22bottom. If we updated the character
  1667. 1:11:24after calling the variable dump
  1668. 1:11:26function, the function would output the
  1669. 1:11:28original value of the string. Not taking
  1670. 1:11:30the time to arrange your code can result
  1671. 1:11:33in unexpected behavior. I think it's
  1672. 1:11:36time for an exercise. In the next
  1673. 1:11:38lecture, let's tackle an exercise to
  1674. 1:11:40reinforce what we've learned about data
  1675. 1:11:42types.
  1676. 1:11:44[Music]
  1677. 1:12:04In this lecture, we're going to explore
  1678. 1:12:07another data type called arrays. Arrays
  1679. 1:12:10were designed for storing a collection
  1680. 1:12:12of values. For example, let's say we
  1681. 1:12:15were hired to develop a food tracking
  1682. 1:12:17application. Users may want to store a
  1683. 1:12:19list of their favorite foods. One
  1684. 1:12:22solution would be to store the foods in
  1685. 1:12:24a variable. In our script, let's create
  1686. 1:12:26a variable called food one with a value
  1687. 1:12:29of salad.
  1688. 1:12:33Afterward, I'll create another variable
  1689. 1:12:35called food two with a value of burger.
  1690. 1:12:41I can continuously create variables for
  1691. 1:12:43each of my favorite foods. However, this
  1692. 1:12:46process can be cumbersome. Creating a
  1693. 1:12:49unique variable for each food item is
  1694. 1:12:51not scalable. What if we need to remove
  1695. 1:12:53an item from our list? What if we need
  1696. 1:12:56to add more foods or replace existing
  1697. 1:12:58foods? Managing the variable names is
  1698. 1:13:01tedious as the list grows. Arrays can
  1699. 1:13:04simplify the process. PHP allows
  1700. 1:13:07developers to store a list of values
  1701. 1:13:09from within a single variable. Let's try
  1702. 1:13:12creating an array by completely removing
  1703. 1:13:14these variables except the data
  1704. 1:13:16variable. Next, let's update the data
  1705. 1:13:20variables value to a pair of square
  1706. 1:13:22brackets.
  1707. 1:13:25Arrays are created with a pair of square
  1708. 1:13:28brackets. Inside these brackets, we can
  1709. 1:13:30start writing a list of values. Strings,
  1710. 1:13:33booleans, integers, and floats can be
  1711. 1:13:36inserted within an array. In addition,
  1712. 1:13:39arrays are not constrained to a single
  1713. 1:13:41data type. We can insert a number
  1714. 1:13:44followed by a string. Let's add our list
  1715. 1:13:47of favorite foods as strings. To
  1716. 1:13:49separate each item in an array, we can
  1717. 1:13:52use a comma.
  1718. 1:13:55I've added three items to my array. Feel
  1719. 1:13:58free to use different values. After
  1720. 1:14:00adding our values, we can access a
  1721. 1:14:03specific value using square brackets.
  1722. 1:14:06Below the variable, update the variable
  1723. 1:14:09dump function with the following value,
  1724. 1:14:11data 1.
  1725. 1:14:15You're already familiar with the syntax.
  1726. 1:14:17Items in arrays can be accessed via
  1727. 1:14:19their index. Behind the scenes, PHP
  1728. 1:14:22assigns a number to each item in our
  1729. 1:14:24array. As mentioned before, PHP starts
  1730. 1:14:28counting at zero. Therefore, the first
  1731. 1:14:30item in the array has an index of zero.
  1732. 1:14:33The second item has an index of one. So
  1733. 1:14:36on and so forth. In this example, we're
  1734. 1:14:39accessing the second item from the
  1735. 1:14:41array. As you can see, the second item
  1736. 1:14:43has appeared as the output. What if we
  1737. 1:14:46want to view the entire array? In that
  1738. 1:14:48case, we can omit the square brackets
  1739. 1:14:50from the variable dump function like so.
  1740. 1:14:56This time the entire array gets
  1741. 1:14:58rendered. There are a few things worth
  1742. 1:15:00mentioning. Firstly, the number of items
  1743. 1:15:03in the array is rendered from within the
  1744. 1:15:05parenthesis. Secondly, the data types
  1745. 1:15:08for the items in the array are
  1746. 1:15:10presented. Lastly, the index of each
  1747. 1:15:13item starts from zero and increments by
  1748. 1:15:15one for each item in the array. We're
  1749. 1:15:18not limited to creating an array.
  1750. 1:15:20Sometimes you may want to update items.
  1751. 1:15:23Specific items can be updated through
  1752. 1:15:25the square bracket syntax. Back in our
  1753. 1:15:28script, let's update the second item in
  1754. 1:15:31the array. Type the following with me.
  1755. 1:15:33Data 1 equals chicken.
  1756. 1:15:38Once again, we're accessing a specific
  1757. 1:15:40item from an array. This time, we're
  1758. 1:15:43updating the value that has been
  1759. 1:15:44assigned an index of one. Before testing
  1760. 1:15:47our code, there's one more thing I want
  1761. 1:15:49to show you. New items can be added to
  1762. 1:15:52an array by writing a pair of empty
  1763. 1:15:54square brackets. For demonstration
  1764. 1:15:56purposes, let's add a new item to our
  1765. 1:15:59array.
  1766. 1:16:02By not accessing a specific index, PHP
  1767. 1:16:05adds the value at the end of the array.
  1768. 1:16:08Looking at the output, the new item has
  1769. 1:16:10an index of three. In addition, the
  1770. 1:16:13array size has changed from three to
  1771. 1:16:15four. Adding a new item to the array has
  1772. 1:16:18increased its size. PHP was able to
  1773. 1:16:22successfully add a new item to our
  1774. 1:16:24array. Overall, arrays can be a powerful
  1775. 1:16:27feature for storing a collection or list
  1776. 1:16:30of data. If you think about it, most
  1777. 1:16:32applications deal with lists. A list of
  1778. 1:16:35users, posts, settings, and images are
  1779. 1:16:38examples of data that can be stored in
  1780. 1:16:40an array. Arrays have so much more to
  1781. 1:16:43offer. In the next lecture, let's
  1782. 1:16:45continue exploring other features of
  1783. 1:16:47arrays.
  1784. 1:16:49[Music]
  1785. 1:16:59In this lecture, we're going to explore
  1786. 1:17:01an optional feature of arrays called
  1787. 1:17:04named keys. As we know, PHP assigns
  1788. 1:17:07numbers to each item in an array. You
  1789. 1:17:10can refer to these numbers as indexes.
  1790. 1:17:13However, there's another name you can
  1791. 1:17:15use, which is keys. Regardless of what
  1792. 1:17:17you call it, we're not forced to use the
  1793. 1:17:20number system. In some cases, we may
  1794. 1:17:23want to add a name to describe the value
  1795. 1:17:25in an array. Names are considered to be
  1796. 1:17:28more memorable. In addition, they can be
  1797. 1:17:30useful for better describing the data
  1798. 1:17:32stored in an array. PHP supports this
  1799. 1:17:35feature, which is referred to as an
  1800. 1:17:37associative array. Associative arrays
  1801. 1:17:40use names for keys instead of numbers.
  1802. 1:17:43Let's look at an example. In our array,
  1803. 1:17:46we're storing a list of food. What if we
  1804. 1:17:48want to associate each food with a
  1805. 1:17:50specific user? It could be presented as
  1806. 1:17:53a user's favorite food. Before the first
  1807. 1:17:55value, add the following. John equals
  1808. 1:17:58greater than.
  1809. 1:18:02A named key can be assigned by adding
  1810. 1:18:04the double arrow operator. To the left
  1811. 1:18:07of this operator, we can assign a name
  1812. 1:18:09to our key. In this case, we're using
  1813. 1:18:11the name John. to the right of this
  1814. 1:18:14operator. We must specify the value to
  1815. 1:18:17associate with the key by adding a name.
  1816. 1:18:20PHP does not add a numeric index to this
  1817. 1:18:23value. Our array outputs the values
  1818. 1:18:26including the value with the named key.
  1819. 1:18:29Looking closely, the key does not hold a
  1820. 1:18:32number. Rather, it holds the name we
  1821. 1:18:34assigned it during the variable
  1822. 1:18:36declaration. It's completely optional to
  1823. 1:18:39add named keys to all values. If we do
  1824. 1:18:42not set a name for a key, PHP
  1825. 1:18:44automatically defaults to a number.
  1826. 1:18:47However, this practice is uncommon.
  1827. 1:18:49Developers will often stick to numbers
  1828. 1:18:51or named keys in a single array. It's
  1829. 1:18:54better to be consistent. For that
  1830. 1:18:56reason, let's update the array by adding
  1831. 1:18:59names to each item in the array. You can
  1832. 1:19:02use whatever names you'd like. I'll
  1833. 1:19:04quickly update the array.
  1834. 1:19:08In the array, the first three items have
  1835. 1:19:10a name. There's just one problem. Two
  1836. 1:19:13additional items have been added. If we
  1837. 1:19:15look back at our code, we're adding two
  1838. 1:19:17items called chicken and tomato soup.
  1839. 1:19:21Previously, we were updating the item
  1840. 1:19:23with an index of one. However, since we
  1841. 1:19:26have an associative array, there isn't
  1842. 1:19:29an item with this index. If a value
  1843. 1:19:31doesn't exist at a specific index, PHP
  1844. 1:19:34creates the index. That's not what we
  1845. 1:19:37want. We should update the item in the
  1846. 1:19:39array with the key called Jane. Luckily,
  1847. 1:19:42that's easy. Inside the brackets,
  1848. 1:19:44replace the number with the key name.
  1849. 1:19:49This update should effectively update an
  1850. 1:19:51existing item. Below this line of code,
  1851. 1:19:54we're adding a new item to the array.
  1852. 1:19:56Currently, we're not assigning a name.
  1853. 1:19:58Therefore, PHP assigns a number. Let's
  1854. 1:20:01add a name in the square brackets.
  1855. 1:20:06After making those changes, all items in
  1856. 1:20:09the array have names. We can reference a
  1857. 1:20:11specific item in the array by a user's
  1858. 1:20:14name. Associative arrays can be helpful
  1859. 1:20:16when dealing with arrays that have
  1860. 1:20:18limited data. Adding names can be a
  1861. 1:20:21great way to describe the data stored at
  1862. 1:20:23a specific index. In the following
  1863. 1:20:26lecture, let's look at one more feature
  1864. 1:20:28related to arrays.
  1865. 1:20:30[Music]
  1866. 1:20:41In this lecture, we're going to explore
  1867. 1:20:43a feature called multi-dimensional
  1868. 1:20:45arrays. Sounds like a complicated
  1869. 1:20:47feature, but I promise it's not. A
  1870. 1:20:50multi-dimensional array is when an array
  1871. 1:20:52stores another array. Thus, you end up
  1872. 1:20:55having a nested array structure. Nested
  1873. 1:20:58arrays are very common in programming.
  1874. 1:21:00Let's look at an example. In our array,
  1875. 1:21:03each user has one favorite food. But
  1876. 1:21:06what if they have multiple? It would
  1877. 1:21:08make sense to use arrays instead of
  1878. 1:21:10strings for storing this information.
  1879. 1:21:13This way, users can have multiple
  1880. 1:21:15favorite foods. For the first item in
  1881. 1:21:17the array, let's wrap the string with
  1882. 1:21:20square brackets.
  1883. 1:21:23Voila, we have a multi-dimensional
  1884. 1:21:26array. It's an array from within an
  1885. 1:21:29array. There's no limit as to how many
  1886. 1:21:31nested arrays can be created. This
  1887. 1:21:33feature can be handy for creating a
  1888. 1:21:36complex structure of data. I'm going to
  1889. 1:21:38add a second item to the array.
  1890. 1:21:43In the preview, our array has four
  1891. 1:21:45items. The first item stores another
  1892. 1:21:48array. The question is, what if we want
  1893. 1:21:51to access a specific item from a nested
  1894. 1:21:54array? That's simple. We can use square
  1895. 1:21:56bracket syntax. Back in the variable
  1896. 1:21:59dump function, add a pair of square
  1897. 1:22:01brackets to access the item called John.
  1898. 1:22:06Let's try accessing the second item in
  1899. 1:22:09the array by adding an additional pair
  1900. 1:22:11of square brackets. Inside these
  1901. 1:22:13brackets, type the number one.
  1902. 1:22:18Look at that. The second item from the
  1903. 1:22:20nested array has been outputed. That's
  1904. 1:22:22exactly what we were looking for. What
  1905. 1:22:24do you think would happen if we tried
  1906. 1:22:26accessing a nested item from an array
  1907. 1:22:29that doesn't exist? Let's find out. In
  1908. 1:22:32the variable dump function, change the
  1909. 1:22:34value from within the first pair of
  1910. 1:22:36square brackets to Bob.
  1911. 1:22:40Currently, we don't have an item in our
  1912. 1:22:43array with a key called Bob. As
  1913. 1:22:45expected, an error gets produced.
  1914. 1:22:48Typically, one error would appear. This
  1915. 1:22:50time, PHP is rendering two errors. Let's
  1916. 1:22:53dissect them. The first error is an
  1917. 1:22:56undefined error. Whenever you see the
  1918. 1:22:58word undefined, it means that a variable
  1919. 1:23:01does not exist. Your script is
  1920. 1:23:03attempting to access a variable that
  1921. 1:23:06hasn't been defined or is inaccessible.
  1922. 1:23:09This error makes complete sense in the
  1923. 1:23:11variable dump function or attempting to
  1924. 1:23:14access an item with a key called Bob,
  1925. 1:23:17which doesn't exist. As for the second
  1926. 1:23:19error, the description states the
  1927. 1:23:21following. trying to access array offset
  1928. 1:23:24on value of type null. The error states
  1929. 1:23:27that we're trying to access an array. If
  1930. 1:23:30we look at our code, we're trying to
  1931. 1:23:32access the second item from an array
  1932. 1:23:34called Bob. However, this array doesn't
  1933. 1:23:37exist. If an array doesn't exist, square
  1934. 1:23:40bracket syntax does not work. PHP states
  1935. 1:23:43that we're trying to use an array that
  1936. 1:23:46doesn't exist. Hopefully, that makes
  1937. 1:23:48sense. It's a possible error you might
  1938. 1:23:50encounter whenever working with
  1939. 1:23:52multi-dimensional arrays. You can't
  1940. 1:23:54access a nested array if an incorrect
  1941. 1:23:57item from the list is selected. That
  1942. 1:23:59wraps up multi-dimensional arrays.
  1943. 1:24:02Actually, that wraps up our discussion
  1944. 1:24:04on the various data types. There are
  1945. 1:24:06other data types worth exploring, but
  1946. 1:24:09we'll get to those in the future. For
  1947. 1:24:11now, I think it's time we start shifting
  1948. 1:24:13our focus to another subject. In the
  1949. 1:24:15next lecture, let's start talking about
  1950. 1:24:17type casting.
  1951. 1:24:19[Music]
  1952. 1:24:29In this lecture, we're going to explore
  1953. 1:24:32type casting. From time to time, you may
  1954. 1:24:34need to change the data type of an
  1955. 1:24:36existing value. It's not uncommon to
  1956. 1:24:39perform this type of action. PHP refers
  1957. 1:24:42to this type of behavior as type
  1958. 1:24:44casting. Typ casting is the process of
  1959. 1:24:47converting the data type of a value to a
  1960. 1:24:49different data type. Overall, PHP makes
  1961. 1:24:52it extremely simple to change data
  1962. 1:24:55types. Type casting can be performed by
  1963. 1:24:57wrapping the new type with parenthesis.
  1964. 1:25:00Afterward, you must provide the value to
  1965. 1:25:03change. In this example, I'm
  1966. 1:25:05demonstrating type casting on a single
  1967. 1:25:07variable. It's cast into a boolean,
  1968. 1:25:10integer, float, string, and an array.
  1969. 1:25:13While type casting is powerful, the
  1970. 1:25:16results can be unexpected.
  1971. 1:25:19In the resource section of this lecture,
  1972. 1:25:21I provide a link to a gist. If you're
  1973. 1:25:24not familiar with GitHub, that's
  1974. 1:25:26perfectly fine. GitHub is a resource for
  1975. 1:25:28sharing code with others. In a future
  1976. 1:25:31lecture, we're going to have an
  1977. 1:25:32opportunity to learn more about GitHub.
  1978. 1:25:35For now, all you need to know is that
  1979. 1:25:37I'll be using it to share code with you.
  1980. 1:25:39To prevent the lecture from becoming
  1981. 1:25:41repetitive, I've decided to provide an
  1982. 1:25:44entire example of type casting of
  1983. 1:25:46various types and values. Copy this
  1984. 1:25:49entire piece of code to your clipboard.
  1985. 1:25:53Next, switch over to our project. Inside
  1986. 1:25:56the index.php file, replace the current
  1987. 1:26:00contents of the file with the code in
  1988. 1:26:02your clipboard. Be sure not to replace
  1989. 1:26:04the opening PHP tags, otherwise this
  1990. 1:26:08code would not work.
  1991. 1:26:11All right, let's take the time to
  1992. 1:26:13understand what's going on. If we look
  1993. 1:26:15through the code, we are repeatedly
  1994. 1:26:17calling the variable dump function. The
  1995. 1:26:20main difference is the value passed into
  1996. 1:26:22the function. Various values are being
  1997. 1:26:25type cast. Let's start from the top and
  1998. 1:26:27work our way down. The first set of
  1999. 1:26:30examples is non boolean values being
  2000. 1:26:33casted into boolean values. We can use
  2001. 1:26:35the bool keyword to perform this
  2002. 1:26:37operation. Alternatively, we can type
  2003. 1:26:40boolean as an example. The second
  2004. 1:26:43variable dump function uses the boolean
  2005. 1:26:46keyword instead of the bool keyword. I
  2006. 1:26:48prefer to use the shorter version. Let's
  2007. 1:26:51go through these examples one by one.
  2008. 1:26:53Strings can be typecasted into booleans.
  2009. 1:26:56However, that doesn't mean all strings
  2010. 1:26:59are converted to the same value. Empty
  2011. 1:27:01strings are considered to be false. If
  2012. 1:27:04the string contains text, the value
  2013. 1:27:06becomes true. The exception to this rule
  2014. 1:27:09is the third example, which is a string
  2015. 1:27:11with the number zero. On the other hand,
  2016. 1:27:14if the text says false, the value is
  2017. 1:27:17casted into true. Moving on to integers
  2018. 1:27:20and floats, numbers are set to true when
  2019. 1:27:23the number is not zero. This includes
  2020. 1:27:25negative numbers too. If the number is
  2021. 1:27:28zero, the value becomes false. Even
  2022. 1:27:31though these examples are integers, the
  2023. 1:27:33same rules apply to floats. Up next,
  2024. 1:27:36null and empty arrays are considered to
  2025. 1:27:39be false. On the other hand, if the
  2026. 1:27:41array contains one or more items, the
  2027. 1:27:44value becomes true. It doesn't matter
  2028. 1:27:47what the contents of the array are. As
  2029. 1:27:49long as the array has values, the array
  2030. 1:27:51is converted into true. You can confirm
  2031. 1:27:54these values in the preview. They should
  2032. 1:27:57match the values written in the
  2033. 1:27:59comments. If it's hard to read these
  2034. 1:28:01values, feel free to add break lines
  2035. 1:28:03into the output using HTML. Let's move
  2036. 1:28:07on to type casting values to integers.
  2037. 1:28:10Just so that the preview doesn't get
  2038. 1:28:12cluttered with a bunch of values, I've
  2039. 1:28:14applied comments to the other examples.
  2040. 1:28:16I'm going to comment the boolean
  2041. 1:28:18examples and uncomment the integer
  2042. 1:28:21examples. Here's a cool trick to
  2043. 1:28:23instantly apply or remove comments from
  2044. 1:28:26multiple lines of code. If you were to
  2045. 1:28:28select multiple lines of code and then
  2046. 1:28:30press the keyboard shortcut control
  2047. 1:28:34slash, comments are toggled on the
  2048. 1:28:36selected lines. For Mac users, this
  2049. 1:28:39shortcut would be command slash.
  2050. 1:28:44All right. Values can be typecasted to
  2051. 1:28:47integers with the int keyword.
  2052. 1:28:49Alternatively, you can use the integer
  2053. 1:28:52keyword which is shown in the second
  2054. 1:28:54example. However, like the boolean
  2055. 1:28:56example, I prefer the shorter version.
  2056. 1:28:59In the first few examples, boolean
  2057. 1:29:01values are casted into integers. A false
  2058. 1:29:04boolean is set to zero, whereas a true
  2059. 1:29:07boolean value is set to one. If we're
  2060. 1:29:10updating strings, PHP removes
  2061. 1:29:13non-numeric characters from the string.
  2062. 1:29:16For example, a string with a number
  2063. 1:29:18minus one is set to minus1. However, if
  2064. 1:29:21our string doesn't contain numbers, the
  2065. 1:29:23default value is zero. What if we have a
  2066. 1:29:26combination of numbers or characters?
  2067. 1:29:28Non-numeric characters are removed,
  2068. 1:29:31leaving us with the number. If we
  2069. 1:29:33convert a float to an integer, the
  2070. 1:29:35decimal value is wholly removed. This is
  2071. 1:29:38important to understand. PHP does not
  2072. 1:29:41round the value. numbers to the right of
  2073. 1:29:43the dot character are removed. If we
  2074. 1:29:46were to round this value, the result
  2075. 1:29:48would be 13. However, if you look at the
  2076. 1:29:50output, the value is 12. Understanding
  2077. 1:29:53this behavior can be a small gotcha if
  2078. 1:29:56you're not aware of it. Lastly, a null
  2079. 1:29:58value is set to zero. Let's move on to
  2080. 1:30:01the float examples. Similar to before,
  2081. 1:30:04comment the integer examples and
  2082. 1:30:06uncomment the float examples.
  2083. 1:30:10values can be typcasted to a float with
  2084. 1:30:13the float keyword. Most of the
  2085. 1:30:15conversions are similar to integers. If
  2086. 1:30:18I'm being completely honest, there's
  2087. 1:30:20nothing new to share. So, let's just
  2088. 1:30:22move on to the next data type. Comment
  2089. 1:30:25the float examples and then uncomment
  2090. 1:30:27the string examples.
  2091. 1:30:31Type casting to strings can be performed
  2092. 1:30:33with the string keyword. Let's go
  2093. 1:30:36through the examples. Firstly, a false
  2094. 1:30:39value is set to an empty string. On the
  2095. 1:30:41other hand, a true value is set to one.
  2096. 1:30:44Integers and floats are converted to
  2097. 1:30:47strings without stripping numbers from
  2098. 1:30:49the original value. Basically, PHP just
  2099. 1:30:53wraps your numbers with quotes. Arrays
  2100. 1:30:56are a special case. They can be
  2101. 1:30:58converted, but PHP does throw a warning
  2102. 1:31:00during this process. It's not able to
  2103. 1:31:03completely convert an array to a string.
  2104. 1:31:06All you'll get is the word array. Values
  2105. 1:31:09inside the array are entirely removed.
  2106. 1:31:11If you look at the preview, you'll be
  2107. 1:31:13able to see how they're treated in our
  2108. 1:31:15script. The word array appears, but a
  2109. 1:31:18warning is accompanied with the value.
  2110. 1:31:21Just be aware of this behavior when type
  2111. 1:31:23casting arrays. Lastly, a null value is
  2112. 1:31:27converted into an empty string. That's
  2113. 1:31:29about it for strings. Let's move on to
  2114. 1:31:31the last set of examples. comment the
  2115. 1:31:34string examples and uncomment the array
  2116. 1:31:37examples.
  2117. 1:31:40A value can be typecasted into an array
  2118. 1:31:42with the array keyword. Array type
  2119. 1:31:45casting is straightforward. The value is
  2120. 1:31:48always inserted as the first item in the
  2121. 1:31:50array. It doesn't matter if we're
  2122. 1:31:52working with booleans, strings,
  2123. 1:31:54integers, or floats. The one exception
  2124. 1:31:56to this rule is null values. An empty
  2125. 1:31:59array gets produced for null values.
  2126. 1:32:02That's about it. You can look at the
  2127. 1:32:04preview for the final result of typ
  2128. 1:32:06casting these values. Understanding typ
  2129. 1:32:08casting is essential. Believe it or not,
  2130. 1:32:11PHP can automatically type cast your
  2131. 1:32:14variables without your consent. In the
  2132. 1:32:16following lecture, let's discuss this
  2133. 1:32:18feature.
  2134. 1:32:21[Music]
  2135. 1:32:31In this lecture, we're going to look at
  2136. 1:32:33a feature called type juggling. From
  2137. 1:32:35time to time, you may hear this feature
  2138. 1:32:37referred to as type coercion. They mean
  2139. 1:32:39the same thing. So, what is type
  2140. 1:32:41juggling and why should you care about
  2141. 1:32:43it? There are specific situations where
  2142. 1:32:46PHP attempts to change the data type of
  2143. 1:32:48your values without your explicit
  2144. 1:32:50permission. Even if you're not
  2145. 1:32:52performing type casting, PHP may perform
  2146. 1:32:55type casting automatically. This
  2147. 1:32:57behavior is known as type juggling.
  2148. 1:32:59Believe it or not, we've experienced
  2149. 1:33:01type juggling before.
  2150. 1:33:03In the resource section of this course,
  2151. 1:33:05I provide a link to the echo keyword
  2152. 1:33:08documentation page. Under the
  2153. 1:33:10description section, there's a code
  2154. 1:33:11example of the echo keyword. I didn't
  2155. 1:33:14mention this before, but you can wrap
  2156. 1:33:16the value with a pair of parenthesis.
  2157. 1:33:18However, these characters are completely
  2158. 1:33:20optional. Most developers prefer to skip
  2159. 1:33:23them. The most important part of this
  2160. 1:33:25example is the data type of the value.
  2161. 1:33:28Looking closely, the value after the
  2162. 1:33:30echo keyword should be a string. Isn't
  2163. 1:33:32that strange? We've been able to pass in
  2164. 1:33:34other values such as integers and
  2165. 1:33:36floats. So why does PHP state we must
  2166. 1:33:40pass in a string? Do you remember the
  2167. 1:33:42discussion we had on data types? PHP is
  2168. 1:33:45a dynamically typed language which is
  2169. 1:33:47considered to be beginnerfriendly. One
  2170. 1:33:49of the reasons is because of type
  2171. 1:33:51juggling. If we pass in a value with the
  2172. 1:33:54wrong data type, PHP attempts to rectify
  2173. 1:33:56the situation on our behalf by type
  2174. 1:33:58casting the value. In this case,
  2175. 1:34:01non-strings are type casted into
  2176. 1:34:03strings. Let's look at an example by
  2177. 1:34:05heading over to our project. I'm going
  2178. 1:34:07to remove all the code except the
  2179. 1:34:10opening PHP tag.
  2180. 1:34:13Next, I'm going to echo a random number.
  2181. 1:34:18Despite echoing an integer, the preview
  2182. 1:34:20displays the number perfectly without a
  2183. 1:34:22problem. Once again, PHP is
  2184. 1:34:24automatically type casting the value,
  2185. 1:34:26which is known as type juggling or type
  2186. 1:34:29coercion. It's the same as writing echo
  2187. 1:34:32string.
  2188. 1:34:34At times, this feature can feel
  2189. 1:34:36convenient. It means less code to write.
  2190. 1:34:39On the other hand, it can cause
  2191. 1:34:41unexpected results in your codebase.
  2192. 1:34:43Throughout the course, I'll be sure to
  2193. 1:34:45point out when type juggling can be a
  2194. 1:34:47nuisance. For now, just be aware of this
  2195. 1:34:49feature when working with data. In the
  2196. 1:34:51next lecture, let's shift our focus to
  2197. 1:34:54operators.
  2198. 1:34:56[Music]
  2199. 1:35:06In this lecture, we're going to explore
  2200. 1:35:08arithmetic operators for numbers. In the
  2201. 1:35:11resource section of this lecture, I
  2202. 1:35:13provide a link to a documentation page
  2203. 1:35:15on arithmetic operators. PHP supports
  2204. 1:35:18basic mathematical operations. Addition,
  2205. 1:35:21subtraction, multiplication, and
  2206. 1:35:23division can be performed with PHP. This
  2207. 1:35:26documentation page displays a complete
  2208. 1:35:29list of operators for performing these
  2209. 1:35:31operations. Let's give them a try by
  2210. 1:35:33heading over to our project. I'm going
  2211. 1:35:36to replace the echo statement with a
  2212. 1:35:38variable called data. It'll hold the
  2213. 1:35:40following value 1 + 2 - 3 asterisk 4/5.
  2214. 1:35:49In this example, I'm using some of the
  2215. 1:35:51arithmetic operators. The other
  2216. 1:35:53operators will be introduced in a
  2217. 1:35:55moment. Most of the operators are
  2218. 1:35:57self-explanatory. The plus character
  2219. 1:36:00performs addition. The minus character
  2220. 1:36:02performs subtraction. The asterisk
  2221. 1:36:04character performs multiplication. And
  2222. 1:36:07lastly, the slash character performs
  2223. 1:36:10division. Similar to math, the order of
  2224. 1:36:12operations is applicable to our
  2225. 1:36:14equation. PHP performs multiplication
  2226. 1:36:17followed by division, addition, and
  2227. 1:36:20subtraction. The result will be stored
  2228. 1:36:22in the variable. This is important to
  2229. 1:36:24understand. PHP always executes
  2230. 1:36:27mathematical operations before assigning
  2231. 1:36:29a value to a variable. Take a moment to
  2232. 1:36:32figure out the value that will be
  2233. 1:36:34computed by this equation. Let's log the
  2234. 1:36:36value with the variable dump function.
  2235. 1:36:41As you can see, the data type has been
  2236. 1:36:43set to float with a value of 0.6. The
  2237. 1:36:47value is abnormal. Technically, the
  2238. 1:36:49result should be 0.6. There shouldn't be
  2239. 1:36:52a chain of zeros after the number. So,
  2240. 1:36:54why are we receiving an abnormal value?
  2241. 1:36:59First, let's revisit the concept of
  2242. 1:37:02machine code. Machine code is a
  2243. 1:37:04low-level language understood by
  2244. 1:37:06machines. Everything is written with
  2245. 1:37:08zeros and ones. As you can probably
  2246. 1:37:10guess, machine code can be a nightmare
  2247. 1:37:13to read and write. While it's possible
  2248. 1:37:15to create applications with machine
  2249. 1:37:17code, it would take a lot of work to
  2250. 1:37:19create a simple app. For this reason,
  2251. 1:37:21programming languages were introduced.
  2252. 1:37:23Readability is the goal of most
  2253. 1:37:25languages on the web. At the end of the
  2254. 1:37:27day, our computers can't understand any
  2255. 1:37:29programming language except machine
  2256. 1:37:31code. It doesn't matter if you're using
  2257. 1:37:33PHP, C++, or JavaScript. All programming
  2258. 1:37:37languages cannot be understood by a
  2259. 1:37:39machine. Converting a language to
  2260. 1:37:41another language is known as
  2261. 1:37:42compilation. Programming languages can
  2262. 1:37:45use a compiler or interpreter to perform
  2263. 1:37:47this process. In the case of PHP, an
  2264. 1:37:50interpreter is used to perform this
  2265. 1:37:52process. While there are differences
  2266. 1:37:55between a compiler and an interpreter,
  2267. 1:37:57those differences don't matter to us for
  2268. 1:38:00the time being. The result is the same.
  2269. 1:38:02Our PHP code is being compiled into
  2270. 1:38:05machine code. After it's been compiled
  2271. 1:38:07into machine code, our machines can
  2272. 1:38:10execute the instructions written from
  2273. 1:38:12our script. So, you may be wondering why
  2274. 1:38:14is this process important to know?
  2275. 1:38:17Unfortunately, the compilation process
  2276. 1:38:19is not perfect. PHP struggles with
  2277. 1:38:22decimal values. Whenever working with
  2278. 1:38:24decimal numbers, PHP tends to round your
  2279. 1:38:27values incorrectly.
  2280. 1:38:31In the resource section of this lecture,
  2281. 1:38:33I provide a link to a site called
  2282. 1:38:35floating point guide. Issues with
  2283. 1:38:38floating numbers are not specific to
  2284. 1:38:40PHP. If you decide to switch to another
  2285. 1:38:43language, you're likely to encounter
  2286. 1:38:45this issue. This site is a helpful
  2287. 1:38:47resource for developers to understand
  2288. 1:38:50why floats are a problem. You don't have
  2289. 1:38:52to read it. My explanation is more than
  2290. 1:38:54enough to understand why this behavior
  2291. 1:38:57occurs. However, if you're curious for
  2292. 1:38:59more information, this resource can
  2293. 1:39:01provide additional details. In the
  2294. 1:39:03future, we're going to explore solutions
  2295. 1:39:06for addressing these issues. For now,
  2296. 1:39:08we'll leave the results alone. Back in
  2297. 1:39:10our project, let's explore the other
  2298. 1:39:13arithmetic operators. For the data
  2299. 1:39:16variable, replace the value with the
  2300. 1:39:18following equation. 11%
  2301. 1:39:212.
  2302. 1:39:24The percent character is known as the
  2303. 1:39:26modulo operator. Sometimes you may not
  2304. 1:39:29be able to divide numbers perfectly.
  2305. 1:39:31It's always possible that you may have a
  2306. 1:39:33remainder. The modulo operator performs
  2307. 1:39:36division but it doesn't return the
  2308. 1:39:38result. It returns the remainder. In
  2309. 1:39:41this example, the remainder is one which
  2310. 1:39:43is shown in the preview. It seems like a
  2311. 1:39:46strange operator, but this operator is
  2312. 1:39:48useful when trying to check if numbers
  2313. 1:39:51are evenly divisible or detecting odd
  2314. 1:39:53numbers. The last operator that I want
  2315. 1:39:56to show you is the exponentiation
  2316. 1:39:58operator. As the name suggests, it
  2317. 1:40:01allows us to perform exponentiation with
  2318. 1:40:03numbers. Let's replace the data
  2319. 1:40:06variables value once more with the
  2320. 1:40:08following value 10 asterisk asterisk 2.
  2321. 1:40:14The exponentiation operator is performed
  2322. 1:40:17with two asterisk characters. It's not
  2323. 1:40:20to be confused with the multiplication
  2324. 1:40:22operator which is just one asterisk. The
  2325. 1:40:25number on the left side of the operator
  2326. 1:40:27is considered to be the base whereas the
  2327. 1:40:30number on the right is the exponent. If
  2328. 1:40:32we look at the preview, we get 100.
  2329. 1:40:35Perfect. There's one more thing I want
  2330. 1:40:37to discuss before moving on. I'm going
  2331. 1:40:39to revert the value of the data operator
  2332. 1:40:42to the first equation we had at the
  2333. 1:40:44beginning of the lecture. As we learned
  2334. 1:40:47earlier, the order of operations is
  2335. 1:40:49applied to our equation. We have the
  2336. 1:40:51option of changing the order of
  2337. 1:40:53operations by using parenthesis. For
  2338. 1:40:56example, let's wrap the middle numbers
  2339. 1:40:58with a pair of parenthesis.
  2340. 1:41:02By doing so, PHP calculates the value
  2341. 1:41:05from within the parenthesis before
  2342. 1:41:07executing the other portions of the
  2343. 1:41:09equation. By adding these parenthesis, a
  2344. 1:41:12completely different result gets
  2345. 1:41:14produced. You can fine-tune your
  2346. 1:41:17equations with this simple syntax. As
  2347. 1:41:19long as you know basic math operations,
  2348. 1:41:22you shouldn't have a problem with them
  2349. 1:41:23in PHP. In the next lecture, let's
  2350. 1:41:26continue exploring other operators.
  2351. 1:41:31[Music]
  2352. 1:41:38In this lecture, we're going to continue
  2353. 1:41:40our exploration of operators. In the
  2354. 1:41:43resource section of this lecture, I
  2355. 1:41:45provide a link to the documentation page
  2356. 1:41:47for a complete list of assignment
  2357. 1:41:49operators. Thus far, we've looked at a
  2358. 1:41:51single assignment operator which is
  2359. 1:41:53written with the equals character.
  2360. 1:41:55However, this is not the only assignment
  2361. 1:41:58operator offered by PHP. Other
  2362. 1:42:01assignment operators are available that
  2363. 1:42:03can be thought of as shorthand syntax
  2364. 1:42:05for common operations. Let's look at a
  2365. 1:42:08few examples in our project. Let's
  2366. 1:42:10assign the data variable to 10.
  2367. 1:42:15As we know, arithmetic operators allow
  2368. 1:42:18us to perform mathematical operations
  2369. 1:42:20from within our script. It's not
  2370. 1:42:22uncommon to update a variable's value
  2371. 1:42:25throughout the script. For example,
  2372. 1:42:27let's say the data variable stores the
  2373. 1:42:30total cost of a user's purchase. If
  2374. 1:42:32users remove or add items, the data
  2375. 1:42:35variable should be updated to reflect
  2376. 1:42:38those changes. After setting the data
  2377. 1:42:40variable, let's update the variable to
  2378. 1:42:43the following value, data + 4.
  2379. 1:42:47In this example, we're referencing the
  2380. 1:42:50variable from within the value. PHP
  2381. 1:42:52takes the current value and adds four,
  2382. 1:42:55resulting in 14. There's actually a
  2383. 1:42:57shorter way of writing this line of
  2384. 1:42:59code. PHP introduces variations of the
  2385. 1:43:02assignment operator for each of the
  2386. 1:43:04arithmetic operators. There's a shorter
  2387. 1:43:07way of writing this equation by adding
  2388. 1:43:09the addition operator to the left side
  2389. 1:43:11of the assignment operator. After adding
  2390. 1:43:14this operator, we can remove the data
  2391. 1:43:16variable from the equation.
  2392. 1:43:20The results are the same. By adding the
  2393. 1:43:22addition operator to the left of the
  2394. 1:43:24assignment operator, PHP takes the
  2395. 1:43:27existing value of the data variable and
  2396. 1:43:29adds the value to the operator's right.
  2397. 1:43:32This feature applies to all arithmetic
  2398. 1:43:34operators. We can use subtraction,
  2399. 1:43:39multiplication,
  2400. 1:43:42division,
  2401. 1:43:45modulo,
  2402. 1:43:48and finally exponentiation.
  2403. 1:43:53These assignment operators can help make
  2404. 1:43:55your code look more readable. It's up to
  2405. 1:43:57you if you want to use them. In the next
  2406. 1:43:59lecture, let's keep exploring other
  2407. 1:44:02operators offered by PHP.
  2408. 1:44:04[Music]
  2409. 1:44:15In this lecture, we're going to explore
  2410. 1:44:17another set of operators called
  2411. 1:44:19comparison operators. As the name
  2412. 1:44:22implies, comparison operators are
  2413. 1:44:24capable of comparing values. In the
  2414. 1:44:26resource section of this lecture, I
  2415. 1:44:29provide a link to a complete list of
  2416. 1:44:31comparison operators. One thing to keep
  2417. 1:44:34in mind is the results of the comparison
  2418. 1:44:36operators. Arithmetic operators produced
  2419. 1:44:39integers and floats whereas comparison
  2420. 1:44:41operators produce boolean values. Under
  2421. 1:44:44the results column, take notice of the
  2422. 1:44:46data type. It's all booleans. So let's
  2423. 1:44:50try using some of these operators inside
  2424. 1:44:52our script. We're going to use the
  2425. 1:44:54operators from within the variable dump
  2426. 1:44:57function. We don't need the data
  2427. 1:44:59variable anymore. PHP introduces two
  2428. 1:45:02operators for comparing values. The
  2429. 1:45:04first operator is written with two
  2430. 1:45:06equals characters. This operator
  2431. 1:45:09compares two values and returns a
  2432. 1:45:11boolean. For example, let's try
  2433. 1:45:13comparing one with one.
  2434. 1:45:17It's important to note that we're using
  2435. 1:45:19two equals characters. A common mistake
  2436. 1:45:22among beginners is to use a single
  2437. 1:45:24equals character, which as we know is
  2438. 1:45:27the assignment operator. We don't want
  2439. 1:45:29to assign a value but compare two
  2440. 1:45:31values. In the preview, the operator has
  2441. 1:45:34produced a true boolean value. Now,
  2442. 1:45:37there's something worth noting about the
  2443. 1:45:39equal operator, which is what this
  2444. 1:45:41operator is called. Behind the scenes,
  2445. 1:45:43PHP may perform type juggling. As a
  2446. 1:45:46reminder, type juggling is a feature
  2447. 1:45:49where PHP performs type casting
  2448. 1:45:51automatically. If we attempt to compare
  2449. 1:45:54two values with different data types,
  2450. 1:45:56PHP performs type casting before
  2451. 1:45:59comparing them. For example, let's dump
  2452. 1:46:02another value by adding a comma. The
  2453. 1:46:04variable dump function is not limited to
  2454. 1:46:06rendering a single value. Multiple
  2455. 1:46:09values can be rendered by separating
  2456. 1:46:11them with commas. For the second value,
  2457. 1:46:13let's compare one with a string that
  2458. 1:46:16contains one.
  2459. 1:46:19Technically, these are different values
  2460. 1:46:21because of their data types. However,
  2461. 1:46:23looking over the output, the value has
  2462. 1:46:26been set to true. This result confirms
  2463. 1:46:29the PHP's type juggling behavior.
  2464. 1:46:32Otherwise, these values would not be
  2465. 1:46:34comparable. While it may seem
  2466. 1:46:36convenient, unexpected results may be
  2467. 1:46:38produced. In most cases, developers
  2468. 1:46:41prefer to compare values with the same
  2469. 1:46:43data types. This produces the most
  2470. 1:46:45reliable results. PHP allows us to
  2471. 1:46:48compare values and data types by using
  2472. 1:46:51three equals characters. Let's add an
  2473. 1:46:54extra equals character to both values
  2474. 1:46:56ducted into the output.
  2475. 1:47:00This operator is more strict than the
  2476. 1:47:02previous operator. The data types must
  2477. 1:47:05match otherwise a false value is
  2478. 1:47:08evaluated. In the preview, the results
  2479. 1:47:10are different. This time the second
  2480. 1:47:13boolean value is false. Despite having
  2481. 1:47:16similar values, the data types don't
  2482. 1:47:18match, meaning that PHP considers them
  2483. 1:47:20to be different values. I always
  2484. 1:47:23recommend strictly comparing values.
  2485. 1:47:25Trust me, you'll save yourself a
  2486. 1:47:27headache. Type juggling in these
  2487. 1:47:29scenarios is better left off avoided. Up
  2488. 1:47:32next, we have operators for comparing
  2489. 1:47:34values that don't match. In our
  2490. 1:47:36examples, I'm going to replace the first
  2491. 1:47:39equals character with an exclamation
  2492. 1:47:41point character.
  2493. 1:47:45These operators are referred to as the
  2494. 1:47:47not equal and not identical operators
  2495. 1:47:50respectively. Unlike before, these
  2496. 1:47:52operators attempt to check that the
  2497. 1:47:54values don't match. If they don't match,
  2498. 1:47:57the operator produces a true boolean
  2499. 1:47:59value, otherwise we'll get false. The
  2500. 1:48:02main difference between them is that the
  2501. 1:48:04not equal operator may perform type
  2502. 1:48:07juggling whereas the not identical
  2503. 1:48:09operator does not perform type juggling.
  2504. 1:48:12In the preview, the first comparison
  2505. 1:48:14produces false because one and one
  2506. 1:48:17completely match. However, the second
  2507. 1:48:19comparison produces true since the
  2508. 1:48:22values are different data types. One
  2509. 1:48:24thing I want to mention is that there's
  2510. 1:48:26an alternative syntax for the not equal
  2511. 1:48:29operator. You can use the exclamation
  2512. 1:48:31point equals characters or the less than
  2513. 1:48:35greater than characters.
  2514. 1:48:38There isn't a clear difference between
  2515. 1:48:40these variations. However, the
  2516. 1:48:42exclamation point equals characters are
  2517. 1:48:45the most common combination. Let's keep
  2518. 1:48:47going. Inside the variable dump
  2519. 1:48:49function, we can compare values that are
  2520. 1:48:52less than or greater than each other.
  2521. 1:48:54Add the following values. 2 less than 3,
  2522. 1:48:572 greater than 3, 2 less than equals 3,
  2523. 1:49:012 greater than equals 3.
  2524. 1:49:06The first two examples are
  2525. 1:49:07self-explanatory. The less than
  2526. 1:49:09character is used for checking if one
  2527. 1:49:11value is less than another. Whereas the
  2528. 1:49:13greater than character is used for
  2529. 1:49:15checking if one value is greater than
  2530. 1:49:17another. But what about the other two
  2531. 1:49:19operators? They're similar to the first
  2532. 1:49:22two examples. However, they will also
  2533. 1:49:24check if two values are equal to one
  2534. 1:49:27another. In the third example, we're
  2535. 1:49:29checking if two is less than or equal to
  2536. 1:49:32three. As long as the number on the left
  2537. 1:49:34is less than or equal to three, PHP
  2538. 1:49:37produces a true boolean value. The same
  2539. 1:49:40idea applies to the fourth example, but
  2540. 1:49:42with a greater than comparison. You can
  2541. 1:49:45play around with these operators and
  2542. 1:49:47check out the results to understand how
  2543. 1:49:49they work. For now, that about does it
  2544. 1:49:51for the comparison operators. Let's keep
  2545. 1:49:54going in the next lecture.
  2546. 1:49:56[Music]
  2547. 1:50:06In this lecture, we're going to explore
  2548. 1:50:08another operator or suppressing errors.
  2549. 1:50:11Errors are a common occurrence in
  2550. 1:50:13programming. You should always try your
  2551. 1:50:15best to resolve an error. However, you
  2552. 1:50:17may find yourself short on time. If
  2553. 1:50:19that's the case, it's possible to
  2554. 1:50:21suppress errors with the error control
  2555. 1:50:24operator. Let's look at an example we're
  2556. 1:50:27already familiar with. In the variable
  2557. 1:50:29dump function, replace the value passed
  2558. 1:50:32into the function with the following
  2559. 1:50:33code. String left bracket one right
  2560. 1:50:37bracket.
  2561. 1:50:40In this example, we're type casting an
  2562. 1:50:42array to a string. PHP doesn't like
  2563. 1:50:45this. As you can see from the preview,
  2564. 1:50:47we get a warning from our code. Despite
  2565. 1:50:49the warning, PHP has typcasted the array
  2566. 1:50:52into the word array. If we're short on
  2567. 1:50:55time, we may not have time to properly
  2568. 1:50:57address this error. PHP gives us the
  2569. 1:51:00option of suppressing the error by
  2570. 1:51:02adding the at character at the beginning
  2571. 1:51:04of the line of code. If an error gets
  2572. 1:51:06thrown, PHP ignores it. In this example,
  2573. 1:51:10I'm converting an array into a string,
  2574. 1:51:12which is not allowed. Typically, this
  2575. 1:51:14line of code throws an error. Thanks to
  2576. 1:51:16the error control operator, nothing will
  2577. 1:51:19be output onto the screen. I really want
  2578. 1:51:22to emphasize that you should use this
  2579. 1:51:24operator sparingly. PHP has plenty of
  2580. 1:51:27features to help you avoid errors in
  2581. 1:51:29your code. This operator should almost
  2582. 1:51:31never be necessary. Despite that, it may
  2583. 1:51:34come in handy one day. In the next
  2584. 1:51:36lecture, let's keep exploring other
  2585. 1:51:38operators.
  2586. 1:51:40[Music]
  2587. 1:51:50In this lecture, we're going to learn
  2588. 1:51:52about the increment and decrement
  2589. 1:51:54operators. These operators were
  2590. 1:51:56introduced for updating integers by one.
  2591. 1:51:59Let's look at an example to understand
  2592. 1:52:01how they work. In our script, let's set
  2593. 1:52:04the data variable to a random number
  2594. 1:52:06like five.
  2595. 1:52:09In some cases, we may want to update
  2596. 1:52:12this number by one. For example, you may
  2597. 1:52:14want to keep track of the items in a
  2598. 1:52:16user's cart. If they add an item, you
  2599. 1:52:19may want to update the cart's size by
  2600. 1:52:21one. A possible solution would be to do
  2601. 1:52:24the following. Data equals data + one.
  2602. 1:52:29That works, but we can shorten this code
  2603. 1:52:32even further by using the addition
  2604. 1:52:34assignment operator like so.
  2605. 1:52:38However, there's an even shorter
  2606. 1:52:40solution, which is the increment
  2607. 1:52:42operator. If your goal is to update a
  2608. 1:52:44number by one, you can do the following
  2609. 1:52:47data ++.
  2610. 1:52:50The plus+ characters are known as the
  2611. 1:52:53increment operator. It'll update a value
  2612. 1:52:55by one. Let's update the variable dump
  2613. 1:52:58function to output the data variable.
  2614. 1:53:02In the preview, the new value is six.
  2615. 1:53:05Numbers can be decremented too, which is
  2616. 1:53:08subtracting the current value by one.
  2617. 1:53:10Instead of using the plus plus
  2618. 1:53:12characters, they can be replaced with
  2619. 1:53:14the minus minus characters.
  2620. 1:53:18This time the page displays four. These
  2621. 1:53:21operators are a useful shortcut. There's
  2622. 1:53:23one thing to keep in mind when using
  2623. 1:53:25these operators. The location of the
  2624. 1:53:27operator does affect the behavior of the
  2625. 1:53:30operator. If we add the operators after
  2626. 1:53:33the value, PHP returns the value. After
  2627. 1:53:36doing so, it'll increment the value.
  2628. 1:53:39These steps are not performed together.
  2629. 1:53:41You might want the value to be updated
  2630. 1:53:43before it's returned. For example, we
  2631. 1:53:46may want to increment the value while
  2632. 1:53:48dumping it.
  2633. 1:53:51In the preview, the value remains five
  2634. 1:53:54despite incrementing it in our code.
  2635. 1:53:57This is because PHP is passing on the
  2636. 1:53:59value to the function before
  2637. 1:54:01incrementing it. Therefore, the original
  2638. 1:54:04value gets rendered on the screen. If we
  2639. 1:54:06want to wait for the value to be
  2640. 1:54:08incremented before outputting it, we can
  2641. 1:54:10position the increment operator before
  2642. 1:54:13the variable like so.
  2643. 1:54:17Now the value has been incremented
  2644. 1:54:19before being displayed. This same
  2645. 1:54:21behavior applies to the decrement
  2646. 1:54:23operator. Immediately using a variable
  2647. 1:54:26after it's been updated is common. If
  2648. 1:54:28the variable is not updated properly,
  2649. 1:54:31you may want to consider moving it to
  2650. 1:54:33the other side of the variable. So
  2651. 1:54:35that's the increment and decrement
  2652. 1:54:38operators. Let's keep exploring other
  2653. 1:54:40operators in the next lecture.
  2654. 1:54:43[Music]
  2655. 1:54:53In this lecture, we're going to start
  2656. 1:54:55exploring logical operators. Comparing
  2657. 1:54:58values is a common action within PHP.
  2658. 1:55:01However, you may want to perform
  2659. 1:55:03multiple comparisons. At the moment,
  2660. 1:55:06we're limited to a single comparison.
  2661. 1:55:08Logical operators were introduced to
  2662. 1:55:10allow developers to perform multiple
  2663. 1:55:13comparisons. In our script, let's say we
  2664. 1:55:16wanted to limit our user base to users
  2665. 1:55:18between the ages of 18 and 65. Above the
  2666. 1:55:22data variable, define a variable called
  2667. 1:55:25age with a value of 29.
  2668. 1:55:30Notice how I said the word define.
  2669. 1:55:32Sometimes developers refer to the
  2670. 1:55:35creation of a variable as a variable
  2671. 1:55:37declaration. You may hear developers say
  2672. 1:55:40define a variable or declare a variable.
  2673. 1:55:43They all mean the same thing. Just
  2674. 1:55:45thought you should be aware of that. All
  2675. 1:55:47right, moving along. Let's set the data
  2676. 1:55:50variable to the following. Age greater
  2677. 1:55:52than 18 and age less than 65.
  2678. 1:55:58In addition, let's replace the operator
  2679. 1:56:00from within the variable dump function.
  2680. 1:56:05So the and operator allows us to add an
  2681. 1:56:09additional comparison instead of one.
  2682. 1:56:11This operator requires that both
  2683. 1:56:13comparisons evaluate to true. If both
  2684. 1:56:16comparisons are truthy, the value of the
  2685. 1:56:18data variable becomes a boolean true.
  2686. 1:56:21Otherwise, the value becomes false. For
  2687. 1:56:23example, let's set the age variable to
  2688. 1:56:2615.
  2689. 1:56:29Technically, the age is under 65, but
  2690. 1:56:32the age is not ever 18. Therefore, the
  2691. 1:56:35data variable evaluates to false.
  2692. 1:56:38There's another thing worth mentioning
  2693. 1:56:40about the and operator. The and operator
  2694. 1:56:43can be written with two amperand
  2695. 1:56:46characters or the word and.
  2696. 1:56:50Both solutions are viable. However, most
  2697. 1:56:53developers prefer the amperand
  2698. 1:56:55variation. While the and operator is
  2699. 1:56:58great, you may want either condition to
  2700. 1:57:00be true. For example, let's create a
  2701. 1:57:03variable called permission with a value
  2702. 1:57:06of admin.
  2703. 1:57:09Restricting access to specific pages is
  2704. 1:57:12a common practice. You may not want
  2705. 1:57:14users to view the settings of a site
  2706. 1:57:16unless they have administrative or
  2707. 1:57:18moderation powers. It's unlikely that
  2708. 1:57:21users are going to have both
  2709. 1:57:22permissions. So, we might just want to
  2710. 1:57:25check for one of these permissions. For
  2711. 1:57:27the data variable, let's use the
  2712. 1:57:29following value. permission equals
  2713. 1:57:32equals equals admin or permission equals
  2714. 1:57:35equals equals mod.
  2715. 1:57:40The or operator can be written with the
  2716. 1:57:42word or or with two pipe characters.
  2717. 1:57:48The pipe variation is more common and
  2718. 1:57:50preferred. If one condition evaluates to
  2719. 1:57:53true and the other condition evaluates
  2720. 1:57:55to false, the entire value evaluates to
  2721. 1:57:58true. Overall, the or operator allows
  2722. 1:58:01for either comparison to be truthy for
  2723. 1:58:03the entire line of code to evaluate to a
  2724. 1:58:06truthy value. There's one more logical
  2725. 1:58:09operator variable called the not
  2726. 1:58:11operator. Thus far, everything checks
  2727. 1:58:14for a truthy value. What if you want to
  2728. 1:58:16test for a false value instead of a true
  2729. 1:58:19value? You can do so with the not
  2730. 1:58:21operator, which is written with the
  2731. 1:58:23exclamation point character. You can add
  2732. 1:58:26this character to the beginning of the
  2733. 1:58:28value. For example, let's use the
  2734. 1:58:30following value for the data variable,
  2735. 1:58:33not permission.
  2736. 1:58:36Next, let's leave the permission
  2737. 1:58:38variable as an empty string.
  2738. 1:58:42By using the not operator, PHP
  2739. 1:58:45transforms a value into a boolean before
  2740. 1:58:48checking the value. After doing so, PHP
  2741. 1:58:51checks for a false value instead of a
  2742. 1:58:54true value. If the value is false, the
  2743. 1:58:56operator evaluates a true boolean value.
  2744. 1:59:00As you can see in the preview, the value
  2745. 1:59:02of the data variable is true.
  2746. 1:59:04Technically, empty strings are
  2747. 1:59:06considered to be false. But by using the
  2748. 1:59:09not operator, that'll allow us to check
  2749. 1:59:11for a false value. That's it for logical
  2750. 1:59:14operators. In the next lecture, we're
  2751. 1:59:17going to continue our discussion of
  2752. 1:59:19operators.
  2753. 1:59:20[Music]
  2754. 1:59:31In this lecture, we're going to start
  2755. 1:59:33focusing on operator precedents. PHP
  2756. 1:59:36offers dozens of operators. Best of all,
  2757. 1:59:39we can use various operators together.
  2758. 1:59:42However, that can potentially be an
  2759. 1:59:44issue if we're not careful. We may
  2760. 1:59:46receive unexpected results. How does PHP
  2761. 1:59:49know which operator to use over the
  2762. 1:59:51other? In the resource section of this
  2763. 1:59:54lecture, I provide a link to a
  2764. 1:59:56documentation page called operator
  2765. 1:59:58precedence. On this page, we're
  2766. 2:00:01presented with a table of operators.
  2767. 2:00:03Operators listed at the top of the table
  2768. 2:00:06have higher precedence than operators
  2769. 2:00:08listed lower on the table. If we have a
  2770. 2:00:11value with multiple operators, PHP
  2771. 2:00:14executes the operators according to
  2772. 2:00:16their precedence. For example, the
  2773. 2:00:19multiplication and division operators
  2774. 2:00:22have higher priority than the addition
  2775. 2:00:24and subtraction operators. In our
  2776. 2:00:27script, let's clear the contents of the
  2777. 2:00:29file. In its place, define a variable
  2778. 2:00:32called a with the following value 5 + 2
  2779. 2:00:36asterisk 10.
  2780. 2:00:40In this example, we have three operators
  2781. 2:00:43which are the assignment, addition and
  2782. 2:00:46multiplication operators. As we know,
  2783. 2:00:49the multiplication operator has the
  2784. 2:00:51higher precedence. Therefore, this part
  2785. 2:00:54of the code is evaluated first. This is
  2786. 2:00:56followed by the addition operator. If
  2787. 2:00:59you were to look through the table, the
  2788. 2:01:01assignment operator is listed at the
  2789. 2:01:03bottom of the table. It's safe to
  2790. 2:01:05conclude that the assignment operator
  2791. 2:01:07gets executed last. Hopefully things are
  2792. 2:01:10making sense on how PHP deals with
  2793. 2:01:13multiple operators. Let's take things a
  2794. 2:01:15step further. What if we have operators
  2795. 2:01:18with the same precedence? For example,
  2796. 2:01:21let's replace the addition operator with
  2797. 2:01:24the division operator.
  2798. 2:01:27How does PHP handle these scenarios? It
  2799. 2:01:30turns out PHP has an answer for us. Back
  2800. 2:01:33at the table, there's a column called
  2801. 2:01:36associivity. If operators have the same
  2802. 2:01:38precedence, PHP executes them in the
  2803. 2:01:41order specified under this column. For
  2804. 2:01:44instance, PHP executes these operators
  2805. 2:01:47from left to right. Therefore, we can
  2806. 2:01:50safely assume PHP starts with division.
  2807. 2:01:53After division has been performed,
  2808. 2:01:56multiplication is performed. In some
  2809. 2:01:58cases, you may want the opposite to
  2810. 2:02:00occur. What if we want multiplication to
  2811. 2:02:03be performed first? In that case, we can
  2812. 2:02:06wrap this portion of the value with
  2813. 2:02:08parenthesis.
  2814. 2:02:11Parentheses give us the option to refine
  2815. 2:02:14the order of operations for all
  2816. 2:02:16operators. You should consider using
  2817. 2:02:18parentheses when you aren't getting the
  2818. 2:02:21desired results. On the other hand, not
  2819. 2:02:24all operators support associivity. Let's
  2820. 2:02:27head back to the table. According to the
  2821. 2:02:29table, comparison operators do not have
  2822. 2:02:32associivity. If a group of operators
  2823. 2:02:34don't have associivity, this means they
  2824. 2:02:37can't be used from within a single
  2825. 2:02:39value. For example, let's head back to
  2826. 2:02:42our code. Update the variable's value to
  2827. 2:02:45the following. 5 less than 2, greater
  2828. 2:02:48than 8.
  2829. 2:02:52We can't use the greater than and less
  2830. 2:02:55than operators within a single
  2831. 2:02:57expression. PHP would throw an error at
  2832. 2:02:59us. Keep this in mind when working with
  2833. 2:03:02these operators. There's one last thing
  2834. 2:03:04I want to mention about operator
  2835. 2:03:06precedence. From time to time, PHP
  2836. 2:03:10offers two operators for performing the
  2837. 2:03:12same thing. For example, let's set the
  2838. 2:03:15variable to the following, true and
  2839. 2:03:17false.
  2840. 2:03:20In this scenario, I'm using the amperand
  2841. 2:03:23characters for the AND operator. As we
  2842. 2:03:26know the alternative syntax for the and
  2843. 2:03:28operator is the word and. Both operators
  2844. 2:03:32allow us to write multiple conditions.
  2845. 2:03:34However, there is a difference. The
  2846. 2:03:36difference stems from the operator's
  2847. 2:03:38precedence. After setting the variable,
  2848. 2:03:41let's log the variable with the variable
  2849. 2:03:44dump function.
  2850. 2:03:47As expected, a false value gets
  2851. 2:03:50evaluated. Both conditions must be true
  2852. 2:03:53for a true value to be produced. Since
  2853. 2:03:56the second value is false, we'll receive
  2854. 2:03:58false. What do you think would happen if
  2855. 2:04:01we switched operators? Let's find out.
  2856. 2:04:03Change the operator to and.
  2857. 2:04:08This time the variable has been set to
  2858. 2:04:10true. Isn't that strange? Both operators
  2859. 2:04:13perform the same logical operation. So
  2860. 2:04:16why does the variable have a different
  2861. 2:04:18value? The answer can be found in the
  2862. 2:04:21table. The and operator has a lower
  2863. 2:04:24precedence than the assignment operator.
  2864. 2:04:26Since that's the case, PHP sets the
  2865. 2:04:29variable to true before checking the
  2866. 2:04:32second value. After the variable has
  2867. 2:04:34been set, PHP throws away the second
  2868. 2:04:37portion of the expression. This behavior
  2869. 2:04:39can be a gotcha. To avoid this scenario
  2870. 2:04:42from occurring, you can always use the
  2871. 2:04:44original variation of the operator. This
  2872. 2:04:47variation has a higher precedence than
  2873. 2:04:49the assignment operator. In most cases,
  2874. 2:04:52you should avoid the and, and or
  2875. 2:04:54keywords. You should use their other
  2876. 2:04:57variations since these operators have
  2877. 2:04:59higher precedence, therefore guaranteed
  2878. 2:05:02to run before a value is assigned. For
  2879. 2:05:05the rest of this course, I'll use
  2880. 2:05:07variations with higher precedence.
  2881. 2:05:10[Music]
  2882. 2:05:20In this lecture, we're going to explore
  2883. 2:05:23another type of variable called a
  2884. 2:05:24constants. Variables are flexible. We
  2885. 2:05:27have the option of overwriting the value
  2886. 2:05:30of a variable after it's been declared.
  2887. 2:05:32Let me give an example. First, I'm going
  2888. 2:05:35to comment the code we've written thus
  2889. 2:05:36far. It's starting to get cluttered.
  2890. 2:05:41Now that our PHP file has been
  2891. 2:05:43commented, let's define a new variable
  2892. 2:05:45called full name. The value for this
  2893. 2:05:48variable will be John Smith.
  2894. 2:05:52After defining a variable, it can be
  2895. 2:05:54updated to a different value. For
  2896. 2:05:57example, let's update the variable to
  2897. 2:05:59100.
  2898. 2:06:02After updating the variable, call the
  2899. 2:06:05variable dump function with the full
  2900. 2:06:07name variable.
  2901. 2:06:10the value of the variable has been
  2902. 2:06:12updated correctly. PHP makes it
  2903. 2:06:14extremely easy to update variables. On
  2904. 2:06:17the other hand, this behavior can be
  2905. 2:06:19problematic. Variables can be accessed
  2906. 2:06:22through multiple PHP files. At the
  2907. 2:06:25moment, we're working on a singular PHP
  2908. 2:06:27file. For larger projects, it's common
  2909. 2:06:30practice to split your codebase into
  2910. 2:06:32multiple files. That can lead to issues.
  2911. 2:06:35Variables can be used across multiple
  2912. 2:06:37files. What if you accidentally update a
  2913. 2:06:40variable? An accidental update can lead
  2914. 2:06:42to errors in other files. In some cases,
  2915. 2:06:45you may want your variables to contain
  2916. 2:06:48the same value throughout the lifetime
  2917. 2:06:50of a program. PHP has a feature for
  2918. 2:06:53creating a variable that can't have its
  2919. 2:06:55value changed called a constant. If we
  2920. 2:06:58were to attempt to update a constant
  2921. 2:07:00after it's been created, an error would
  2922. 2:07:02get thrown. Constants are treated as
  2923. 2:07:04read only. Let's try creating a
  2924. 2:07:06constants. Below the full name variable,
  2925. 2:07:09add a keyword called const.
  2926. 2:07:14Using the const keyword instructs PHP to
  2927. 2:07:17create a constant. After the const
  2928. 2:07:19keyword, we must provide a name for a
  2929. 2:07:22variable. Unlike before, we do not need
  2930. 2:07:24to start the variable name with the
  2931. 2:07:26dollar sign character. The other rules
  2932. 2:07:28for creating a variable still apply. We
  2933. 2:07:31can use alpha numeric and underscore
  2934. 2:07:33characters. Special characters are not
  2935. 2:07:36allowed. Lastly, we can't use reserved
  2936. 2:07:38keywords. For example, we can't set the
  2937. 2:07:41name to const.
  2938. 2:07:45By typing this keyword, we're given an
  2939. 2:07:47error. PHP assumes that we're trying to
  2940. 2:07:50create a constant while attempting to
  2941. 2:07:52create a constant. This code leaves the
  2942. 2:07:54language confused. Let's set the name to
  2943. 2:07:57full name.
  2944. 2:08:00The name of the variable uses all
  2945. 2:08:02uppercase letters. This step is
  2946. 2:08:04completely optional. We could have used
  2947. 2:08:06camel casing, Pascal casing, or snake
  2948. 2:08:09casing. However, constants behave
  2949. 2:08:11differently than regular variables. For
  2950. 2:08:14this reason, developers like to use all
  2951. 2:08:16uppercase letters for constants. It
  2952. 2:08:19helps other developers identify the
  2953. 2:08:21variable as a constant. More often than
  2954. 2:08:24not, this naming convention is widely
  2955. 2:08:26adopted by the community. This includes
  2956. 2:08:28the PHP language. In the resource
  2957. 2:08:31section of this lecture, I provide a
  2958. 2:08:33link to PHP's official list of
  2959. 2:08:35predefined constants. You should avoid
  2960. 2:08:38using the same names listed on this
  2961. 2:08:40page. Otherwise, you may get an error
  2962. 2:08:43stating that you're attempting to update
  2963. 2:08:44an existing constant. Luckily, most of
  2964. 2:08:47PHP's constants start with the word PHP.
  2965. 2:08:51As long as you avoid starting your
  2966. 2:08:52constant name with this word, you're
  2967. 2:08:54good to go. Let's head back to our
  2968. 2:08:56editors. We're going to be using all
  2969. 2:08:59uppercase letters for our constant
  2970. 2:09:01names. Once again, not required, but
  2971. 2:09:04recommended. Let's set the value of this
  2972. 2:09:06constant to John Smith.
  2973. 2:09:10Next, we can output the variable by
  2974. 2:09:13updating the variable dump function to
  2975. 2:09:15use the constant.
  2976. 2:09:19To read a value from a constant, we can
  2977. 2:09:21type its name as is. We do not need to
  2978. 2:09:24include the dollar sign character. The
  2979. 2:09:26value of the constant has been outputed.
  2980. 2:09:29As stated before, we cannot change the
  2981. 2:09:31value after it's been declared. For
  2982. 2:09:33example, I'm going to try updating the
  2983. 2:09:36full name constant to a different name.
  2984. 2:09:41An error gets outputed from the program.
  2985. 2:09:43According to PHP, the full name constant
  2986. 2:09:46has already been created. We're not
  2987. 2:09:48allowed to update its value. This is the
  2988. 2:09:50benefit of using a constant. We're
  2989. 2:09:53restricted to the same value for the
  2990. 2:09:55rest of the program. If we attempt to
  2991. 2:09:57update it, our program will stop
  2992. 2:09:59working. PHP will always notify us when
  2993. 2:10:02a constant is already defined. We'll be
  2994. 2:10:04using constants to help us secure our
  2995. 2:10:07script. Once we start working on a real
  2996. 2:10:09project, you'll find out why they can be
  2997. 2:10:11incredibly useful. In the next lecture,
  2998. 2:10:14let's keep exploring more PHP features.
  2999. 2:10:17[Music]
  3000. 2:10:28In this lecture, we're going to look at
  3001. 2:10:30an alternative solution to injecting
  3002. 2:10:32variables into strings called string
  3003. 2:10:34concatenation. As we learned from a
  3004. 2:10:36previous lecture, string interpolation
  3005. 2:10:38can be a great solution to inserting a
  3006. 2:10:41variable into a string. However, string
  3007. 2:10:43interpolation is only available for
  3008. 2:10:45regular variables. Constants are not
  3009. 2:10:48supported in string interpolation. What
  3010. 2:10:50if we need to insert a constant into a
  3011. 2:10:53string? There's an alternative solution
  3012. 2:10:55called string concatenation. In our
  3013. 2:10:58script, we have two constants called
  3014. 2:11:00full name. Let's remove the second copy.
  3015. 2:11:05Next, let's add the following string
  3016. 2:11:07inside the variable dump function.
  3017. 2:11:10Hello, my name is.
  3018. 2:11:14At the end of the string, we may want to
  3019. 2:11:16insert the full name constants. A
  3020. 2:11:18constant can be added using string
  3021. 2:11:21concatenation. String concatenation is
  3022. 2:11:24the process of combining two strings
  3023. 2:11:26into a single string. We can perform
  3024. 2:11:28string concatenation with the
  3025. 2:11:30concatenation operator. This operator
  3026. 2:11:33can be written with the dot character.
  3027. 2:11:35After the string, add this character.
  3028. 2:11:40PHP will combine both strings into a
  3029. 2:11:43single string. The output contains both
  3030. 2:11:46strings. However, there's one problem.
  3031. 2:11:48Space does not separate both strings,
  3032. 2:11:50which makes the sentence look awkward.
  3033. 2:11:53This behavior makes sense when you think
  3034. 2:11:55about it. In the first string, we don't
  3035. 2:11:57have a space at the end of the string.
  3036. 2:11:59The constant does not contain a space at
  3037. 2:12:01the beginning of the string. If we were
  3038. 2:12:03to add a space at the end of the first
  3039. 2:12:05string, this update should resolve our
  3040. 2:12:08errors.
  3041. 2:12:10As you can see, the space has been
  3042. 2:12:12added. It's a common mistake to forget
  3043. 2:12:14to add spaces when concatenating things.
  3044. 2:12:17String concatenation and string
  3045. 2:12:19interpolation are powerful features for
  3046. 2:12:22inserting variables into a string
  3047. 2:12:24whenever possible. I prefer to use
  3048. 2:12:26string interpolation. In my opinion,
  3049. 2:12:29it's easier to read. If I'm dealing with
  3050. 2:12:31constants, I'll use concatenation since
  3051. 2:12:34interpolation does not support
  3052. 2:12:36constants. You may prefer concatenation
  3053. 2:12:39because it can support strings with
  3054. 2:12:41single or double quotes. Other than
  3055. 2:12:43that, it all comes down to preference.
  3056. 2:12:46Just like arithmetic operators, the
  3057. 2:12:48concatenation operator can be used with
  3058. 2:12:50the assignment operator. For example,
  3059. 2:12:53let's outsource the first portion of the
  3060. 2:12:55string to a variable called message.
  3061. 2:13:00Next, let's type the following after
  3062. 2:13:02defining the variable. Message dot
  3063. 2:13:05equals full name.
  3064. 2:13:08Similar to arithmetic operators, we can
  3065. 2:13:11add the concatenation operator to the
  3066. 2:13:13left of the assignment operator. PHP
  3067. 2:13:16will add the string to the existing
  3068. 2:13:18string. This results in the same thing
  3069. 2:13:20as before. Let's update the variable
  3070. 2:13:23dump function to output the message
  3071. 2:13:25variable.
  3072. 2:13:28Perfect. Our message works like before.
  3073. 2:13:30The concatenation assignment operator
  3074. 2:13:33can be useful when you're trying to add
  3075. 2:13:35to an existing string. That is about it
  3076. 2:13:37for this section. We spent a lot of time
  3077. 2:13:40talking about variables and data types.
  3078. 2:13:42Data is an essential component of any
  3079. 2:13:45application you're building, but how can
  3080. 2:13:47we use this data to create dynamic
  3081. 2:13:49pages? That's going to be the focus of
  3082. 2:13:52the next section. When you're ready,
  3083. 2:13:54I'll see you there.
  3084. 2:13:55[Music]
  3085. 2:14:05In this lecture, we're going to quickly
  3086. 2:14:08discuss an important word called
  3087. 2:14:10expressions. Programming can be
  3088. 2:14:12challenging to grasp because of the
  3089. 2:14:14terminology. Learning the terminology of
  3090. 2:14:16a language is half the battle. If you're
  3091. 2:14:19able to understand the lingo used by
  3092. 2:14:21developers, you're well on your way to
  3093. 2:14:23learning a new language. The word
  3094. 2:14:25expression is going to come up a lot
  3095. 2:14:27throughout the course. So, what are
  3096. 2:14:29expressions? An expression is a line of
  3097. 2:14:32code that evaluates to a value. It's as
  3098. 2:14:34simple as that. Believe it or not, we've
  3099. 2:14:37been writing expressions for most of
  3100. 2:14:39this course. For example, when we create
  3101. 2:14:42a variable, PHP doesn't immediately
  3102. 2:14:44assign the value to the variable. Before
  3103. 2:14:47it does, it'll attempt to evaluate the
  3104. 2:14:49value on the right of the assignment
  3105. 2:14:51operator. In this example, this process
  3106. 2:14:54happens immediately because the value is
  3107. 2:14:57ready as is. The number five evaluates
  3108. 2:14:59to five. After the expression has been
  3109. 2:15:02evaluated, the value is assigned to the
  3110. 2:15:05variable. The same behavior occurs for
  3111. 2:15:07mathematical operations. In this
  3112. 2:15:10example, PHP doesn't immediately assign
  3113. 2:15:12a value to the variable. It'll notice
  3114. 2:15:15that we're performing addition. This
  3115. 2:15:17expression gets evaluated before
  3116. 2:15:19anything else happens. After a value has
  3117. 2:15:22been produced, the value gets assigned
  3118. 2:15:24to the variable. Now you may be thinking
  3119. 2:15:27what would be the difference between
  3120. 2:15:29operators and expressions. They have
  3121. 2:15:31similar definitions. Both terms are used
  3122. 2:15:34to describe operations that produce a
  3123. 2:15:37value. Well, an operator refers to
  3124. 2:15:40characters or symbols that perform an
  3125. 2:15:42operation. As we know, we can have
  3126. 2:15:45multiple operators from a single line of
  3127. 2:15:47code. In comparison, expressions refer
  3128. 2:15:50to the entire line of code that produces
  3129. 2:15:53a value. This includes the operators.
  3130. 2:15:56This behavior is important to
  3131. 2:15:58understand. A lot of code is going to
  3132. 2:16:00contain expressions. Anytime you write
  3133. 2:16:02code that produces a value, you've
  3134. 2:16:05written an expression. In the resource
  3135. 2:16:08section of this lecture, I provide a
  3136. 2:16:10link to the echo keyword. Previously, we
  3137. 2:16:13talked about the data type of the value
  3138. 2:16:15expected by this keyword. Now I want to
  3139. 2:16:18shift our focus to the name of the
  3140. 2:16:20variable. Do you notice anything? The
  3141. 2:16:22variable is referred to as an
  3142. 2:16:24expression. PHP's documentation heavily
  3143. 2:16:27uses the word expression, which makes
  3144. 2:16:30sense, right? After all, PHP expects
  3145. 2:16:33some kind of value to be written after
  3146. 2:16:35the echo keyword. We have the freedom to
  3147. 2:16:38calculate a value after the echo
  3148. 2:16:40keyword. Therefore, the code written
  3149. 2:16:42after the echo keyword can be considered
  3150. 2:16:44an expression. Whenever you see the word
  3151. 2:16:47expression in the documentation, just
  3152. 2:16:49know that PHP is saying that you can
  3153. 2:16:51write code that produces a value. In the
  3154. 2:16:54next lecture, let's start using this
  3155. 2:16:56knowledge to help us learn new features
  3156. 2:16:58of PHP.
  3157. 2:17:00[Music]
  3158. 2:17:10In this lecture, we're going to learn
  3159. 2:17:12how to control the flow of a program.
  3160. 2:17:15Thus far, every line of code we've
  3161. 2:17:17written has been executed. We don't have
  3162. 2:17:19control over the flow of logic in our
  3163. 2:17:21program. What if we want to execute code
  3164. 2:17:23when a condition is true? For example,
  3165. 2:17:26specific pages should be locked unless a
  3166. 2:17:28user is logged in. Another example would
  3167. 2:17:31be displaying administration features
  3168. 2:17:33for users with specific privileges.
  3169. 2:17:36There are hundreds of use cases for
  3170. 2:17:38performing logic based on a condition.
  3171. 2:17:40Luckily, PHP provides a solution for
  3172. 2:17:43controlling the flow of logic called
  3173. 2:17:45conditional statements. In the resource
  3174. 2:17:48section of this lecture, I provide a
  3175. 2:17:50link to the if keyword. This feature
  3176. 2:17:52allows us to perform logic based on a
  3177. 2:17:55condition. Let's read the documentation
  3178. 2:17:57together. In the second paragraph, the
  3179. 2:17:59following is stated as described in the
  3180. 2:18:02section about expressions. Expression is
  3181. 2:18:04evaluated to its boolean value. If
  3182. 2:18:07expression evaluates to true, PHP will
  3183. 2:18:10execute statement and if it evaluates to
  3184. 2:18:13false, it'll ignore it. As we learned,
  3185. 2:18:16expressions are lines of code that
  3186. 2:18:18evaluate to a value. The if keyword
  3187. 2:18:21expects a boolean value. This is
  3188. 2:18:23important to understand. PHP does not
  3189. 2:18:26use integers, floats, or strings.
  3190. 2:18:28Booleans are used to determine whether a
  3191. 2:18:31block of code is executed. In a
  3192. 2:18:33nutshell, PHP performs type juggling on
  3193. 2:18:36expressions that aren't booleans to
  3194. 2:18:38convert the value into a boolean. Let's
  3195. 2:18:41dive into the code to understand this
  3196. 2:18:43feature. Switch over to our project. In
  3197. 2:18:46the PHP file, I'm going to remove the
  3198. 2:18:48existing code.
  3199. 2:18:52As always, be sure to leave the opening
  3200. 2:18:54PHP tag intact. Next, imagine a scenario
  3201. 2:18:58of an application that displays a
  3202. 2:19:00student's grade based on their score. In
  3203. 2:19:03the file, define a variable called score
  3204. 2:19:06with an initial value of 95.
  3205. 2:19:11In America, the grading system can
  3206. 2:19:13contain values between 0 and 100. 100 is
  3207. 2:19:17considered passing. Zero is considered
  3208. 2:19:19failing. Our goal is to inform the
  3209. 2:19:22student that they've passed or failed
  3210. 2:19:24based on their score. After the
  3211. 2:19:26variable, add the if keyword.
  3212. 2:19:30Adding this keyword instructs our
  3213. 2:19:32program to check a condition. The
  3214. 2:19:35condition can be written by adding a
  3215. 2:19:37pair of parenthesis. Within these
  3216. 2:19:39parentheses, we must provide an
  3217. 2:19:41expression that evaluates to a boolean
  3218. 2:19:43value. Let's pass in the score variable.
  3219. 2:19:49The score variable has the integer data
  3220. 2:19:52type. Regardless, this is considered a
  3221. 2:19:55valid expression for the if statement.
  3222. 2:19:57The reason is simple. Behind the scenes,
  3223. 2:20:00PHP performs type juggling for values
  3224. 2:20:03passed into the condition. As we know,
  3225. 2:20:05integers are converted into true if the
  3226. 2:20:08number is not zero. Since 95 is not
  3227. 2:20:10zero, the expression evaluates to true.
  3228. 2:20:13If the expression evaluates to true, PHP
  3229. 2:20:16executes a block of code. The block of
  3230. 2:20:19code can be written inside a pair of
  3231. 2:20:21curly brackets. Let's add these
  3232. 2:20:23characters after the parenthesis.
  3233. 2:20:27Inside the brackets, call the variable
  3234. 2:20:30dump function with the following value
  3235. 2:20:33a.
  3236. 2:20:36As a side note, the code inside the
  3237. 2:20:38conditional statement is indented. This
  3238. 2:20:40formatting is not required but
  3239. 2:20:42recommended. It adds clarity that this
  3240. 2:20:45section of code belongs to the
  3241. 2:20:47conditional statement. It's a common
  3242. 2:20:49practice adopted by developers. will be
  3243. 2:20:52following it too. To recap, if the
  3244. 2:20:54expression evaluates to true, the letter
  3245. 2:20:57A should appear. In the preview, the
  3246. 2:20:59letter has been dumped. While that's
  3247. 2:21:01great, it's not exactly what we were
  3248. 2:21:03aiming for. The same grade gets outputed
  3249. 2:21:06regardless of the number. For example,
  3250. 2:21:09let's set the score to one.
  3251. 2:21:13An appropriate letter grade for the
  3252. 2:21:15score would be an F. As it stands, the
  3253. 2:21:18same letter grade appears on the page.
  3254. 2:21:20by itself. Using the score as the
  3255. 2:21:22condition produces undesirable results.
  3256. 2:21:26Luckily, we know how to resolve this
  3257. 2:21:28issue. We can use comparison operators.
  3258. 2:21:31They're perfect for our scenario.
  3259. 2:21:33Instead of using the score, we're going
  3260. 2:21:34to compare the score with a specific
  3261. 2:21:37number. If the score lands above a
  3262. 2:21:39threshold, the appropriate letter grade
  3263. 2:21:41should appear. PHP offers various
  3264. 2:21:44comparison operators. Best of all, all
  3265. 2:21:47comparison operators produce a boolean
  3266. 2:21:49value, which is what the if keyword
  3267. 2:21:52expects. In the conditional statement,
  3268. 2:21:54update the expression to the following
  3269. 2:21:56score greater than 90.
  3270. 2:22:00Let's look at the preview. This time,
  3271. 2:22:03nothing appears. It's because the
  3272. 2:22:05condition was evaluated to false. If the
  3273. 2:22:07if statement receives a false value, the
  3274. 2:22:10block of code nested inside is not
  3275. 2:22:12executed. But what if we want to output
  3276. 2:22:15an alternative message? After the
  3277. 2:22:17closing curly bracket, we can add an
  3278. 2:22:19else block.
  3279. 2:22:22The else keyword is optional. If it's
  3280. 2:22:25added, PHP will execute code inside the
  3281. 2:22:28else block when the condition from the
  3282. 2:22:30if block fails. There is one rule we
  3283. 2:22:32must follow. The else block must be
  3284. 2:22:35added at the end of the if condition. If
  3285. 2:22:38we were to attempt to write code between
  3286. 2:22:40these keywords, PHP would throw an
  3287. 2:22:42error. Inside the else block, let's call
  3288. 2:22:45the variable dump function with the
  3289. 2:22:48following value f.
  3290. 2:22:52We can take this solution further by
  3291. 2:22:54adding an else- if statement. In between
  3292. 2:22:56these blocks of code, add the elseif
  3293. 2:22:59keyword.
  3294. 2:23:02So far, we have one condition. In some
  3295. 2:23:04cases, we may want to write multiple
  3296. 2:23:07conditions. PHP allows developers to
  3297. 2:23:09chain multiple conditions with the LC if
  3298. 2:23:12keyword. Unlike the if and else
  3299. 2:23:15keywords, we can repeat the else if
  3300. 2:23:17keyword as many times as we'd like.
  3301. 2:23:19However, we may only write this keyword
  3302. 2:23:22after an if statement and before an else
  3303. 2:23:25statement. Inside the parenthesis, we
  3304. 2:23:28can write a condition. Let's write the
  3305. 2:23:30following condition. Score greater than
  3306. 2:23:3280.
  3307. 2:23:35Inside the block of code, call the
  3308. 2:23:37variable dump function with the
  3309. 2:23:39following value B.
  3310. 2:23:43Just for fun, let's add another else if
  3311. 2:23:46statement. This time the condition will
  3312. 2:23:48be score greater than 70.
  3313. 2:23:52Lastly, let's output the letter C with
  3314. 2:23:55the variable dump function.
  3315. 2:23:59Believe it or not, there are variations
  3316. 2:24:01of the else if keyword. It's completely
  3317. 2:24:04optional to add a space between the
  3318. 2:24:06words else and if. I'll remove the space
  3319. 2:24:09as a demonstration.
  3320. 2:24:12For most projects, you should stick with
  3321. 2:24:15consistent formatting instead of mixing
  3322. 2:24:17and matching keywords. Let's check out
  3323. 2:24:19our preview. As you can see, the letter
  3324. 2:24:21F has appeared. That's to be expected
  3325. 2:24:24since the score is too low for each
  3326. 2:24:26condition. PHP defaults to the code from
  3327. 2:24:28the else block which doesn't require a
  3328. 2:24:31condition. Let's try updating the score
  3329. 2:24:33to 95.
  3330. 2:24:36Unlike before, the letter A has been
  3331. 2:24:38outputed. But what about the letters B
  3332. 2:24:41and C? PHP always starts from the top.
  3333. 2:24:44If the first condition fails, it'll move
  3334. 2:24:46on to the next condition. It'll continue
  3335. 2:24:49this process in the order the conditions
  3336. 2:24:51are written. Once it finds a condition
  3337. 2:24:53that evaluates to true, it will not
  3338. 2:24:55bother checking other conditions.
  3339. 2:24:58Technically, the other conditions
  3340. 2:25:00evaluate to true. However, since the
  3341. 2:25:02first condition evaluates to true, the
  3342. 2:25:05other conditional blocks of code are
  3343. 2:25:07never executed. Hence why we're getting
  3344. 2:25:09a single letter. Conditional statements
  3345. 2:25:12are extremely powerful. They allow us to
  3346. 2:25:14control the logic flow by executing code
  3347. 2:25:17based on a condition. There's one more
  3348. 2:25:19thing I want to mention before moving
  3349. 2:25:21on. Unlike before, we're not adding the
  3350. 2:25:24semicolon character at the end of each
  3351. 2:25:26block of code. If we're ever using curly
  3352. 2:25:29brackets for blocks of code, the
  3353. 2:25:31semicolon character is not required.
  3354. 2:25:34Using the closing curly bracket will
  3355. 2:25:36suffice. Just thought you should know in
  3356. 2:25:38case you notice that we're omitting the
  3357. 2:25:40semicolon. In the next lecture, let's
  3358. 2:25:43continue exploring other features for
  3359. 2:25:45controlling logic flow.
  3360. 2:25:47[Music]
  3361. 2:25:57In this lecture, we're going to look at
  3362. 2:25:59an alternative solution for controlling
  3363. 2:26:02logic flow. Switch statements are
  3364. 2:26:04another feature of PHP for performing a
  3365. 2:26:07condition. There are a few differences,
  3366. 2:26:09so let's get into them. In the PHP file,
  3367. 2:26:12let's clear the contents.
  3368. 2:26:16Next, add the switch keyword with a pair
  3369. 2:26:18of parenthesis.
  3370. 2:26:21The biggest difference between if
  3371. 2:26:23statements and switch statements are the
  3372. 2:26:25condition. Conditions for if statements
  3373. 2:26:28are flexible. We can compare values any
  3374. 2:26:30way we'd like. For example, we can check
  3375. 2:26:33if one value is greater than another.
  3376. 2:26:36Alternatively, we can check for things
  3377. 2:26:38like the length of a string or if the
  3378. 2:26:40user is logged in. On the other hand,
  3379. 2:26:43switch statements compare values for
  3380. 2:26:45equality. If two values match, a portion
  3381. 2:26:48of the code will be executed. Let's look
  3382. 2:26:51at an example. Above the switch keyword,
  3383. 2:26:54create a variable called payment status
  3384. 2:26:57with a value of one.
  3385. 2:27:01It's common for developers to use
  3386. 2:27:03numbers to represent the status of an
  3387. 2:27:05action. In this example, we may want to
  3388. 2:27:08store a user's payment status. The
  3389. 2:27:10problem with this value is that numbers
  3390. 2:27:12aren't descriptive. We wouldn't output a
  3391. 2:27:15number to the user. Instead, we may want
  3392. 2:27:18to output a friendlier message. This is
  3393. 2:27:21the perfect case for a switch statement.
  3394. 2:27:23Inside the parenthesis, add the payment
  3395. 2:27:26status variable.
  3396. 2:27:30The switch statement accepts a value.
  3397. 2:27:32The value does not need to be a boolean.
  3398. 2:27:35It can be an integer, float, or string.
  3399. 2:27:38Next, inside a pair of curly brackets,
  3400. 2:27:40we must add a keyword called case.
  3401. 2:27:45After the case keyword, we must add a
  3402. 2:27:48value to compare it with a value from
  3403. 2:27:50the switch statements. Let's add one.
  3404. 2:27:53Afterward, we can add a colon character.
  3405. 2:27:56Code written after this character will
  3406. 2:27:58be executed whenever the value from this
  3407. 2:28:00case matches the value passed into the
  3408. 2:28:03switch statement. Let's call the
  3409. 2:28:05variable dump function with the
  3410. 2:28:07following value success.
  3411. 2:28:11If the values match, this means the
  3412. 2:28:13payment was a success. Let's add another
  3413. 2:28:16case. This time, we'll compare the
  3414. 2:28:18variable with the number two. If the
  3415. 2:28:21values match, we'll call the variable
  3416. 2:28:23dump function with the following value,
  3417. 2:28:26pending.
  3418. 2:28:29You'll notice I'm indenting the code.
  3419. 2:28:31This is not necessary, but recommended.
  3420. 2:28:33It helps with readability. Indenting
  3421. 2:28:36your code helps associate lines of code
  3422. 2:28:38with a specific case. What if the
  3423. 2:28:40variable doesn't match our cases? We can
  3424. 2:28:43add a default case by adding the default
  3425. 2:28:46keyword.
  3426. 2:28:48Similar to before, we can add a colon
  3427. 2:28:51character after this keyword. Any code
  3428. 2:28:54written after this character will be
  3429. 2:28:56executed when the other cases fail.
  3430. 2:28:58Let's output the following message.
  3431. 2:29:01Unknown.
  3432. 2:29:03To recap, switch statements attempt to
  3433. 2:29:06compare values for equality. It doesn't
  3434. 2:29:09try to compare values that are less than
  3435. 2:29:11or greater than each other. Within the
  3436. 2:29:14switch statement, we must provide a
  3437. 2:29:16value to match within its various cases.
  3438. 2:29:18We can add as many case statements as
  3439. 2:29:21we'd like. If a match can't be found,
  3440. 2:29:23the default case is executed. Here's
  3441. 2:29:26where things become slightly bizarre.
  3442. 2:29:28All messages from our cases have been
  3443. 2:29:30executed. But why? Whenever PHP comes
  3444. 2:29:33across a match with a case, it'll
  3445. 2:29:36execute all lines of code written after
  3446. 2:29:38it. This includes lines of code written
  3447. 2:29:41after the case. PHP refers to this
  3448. 2:29:44feature as a fall through strategy.
  3449. 2:29:46We're responsible for instructing PHP to
  3450. 2:29:49start running code. This process can be
  3451. 2:29:52interrupted by adding a keyword called
  3452. 2:29:54break. After each variable dump
  3453. 2:29:56function, add this keyword.
  3454. 2:30:00We don't have to add the break keyword
  3455. 2:30:02to the default case. It's redundant to
  3456. 2:30:05add it in since it's the end of the
  3457. 2:30:07switch statement. After adding this
  3458. 2:30:09keyword, things are different. Only the
  3459. 2:30:11success message appears. I know this
  3460. 2:30:14might seem strange, but there's a good
  3461. 2:30:16reason for this behavior. Currently,
  3462. 2:30:18we're limited to matching a single value
  3463. 2:30:20with the expression from the switch
  3464. 2:30:22statement. What if we want multiple
  3465. 2:30:24values to trigger a block of code?
  3466. 2:30:27That's completely possible thanks to the
  3467. 2:30:29fall through strategy implemented in
  3468. 2:30:31switch statements. For example, after
  3469. 2:30:33the second case, add another case for
  3470. 2:30:36the number three.
  3471. 2:30:39Next, let's update the payment status
  3472. 2:30:41variable to two.
  3473. 2:30:45As you can see, the payment was denied.
  3474. 2:30:47Under the second case, there isn't any
  3475. 2:30:50code to execute. However, we're not
  3476. 2:30:52breaking the case either. PHP continues
  3477. 2:30:55to fall through the next line of code.
  3478. 2:30:57By not doing so, the message from the
  3479. 2:30:59third case gets executed. This behavior
  3480. 2:31:02allows us to add multiple cases to
  3481. 2:31:05produce the same outcome, which may come
  3482. 2:31:07in handy. In my opinion, I prefer to use
  3483. 2:31:10if statements. It's completely possible
  3484. 2:31:12to compare two values. However, from
  3485. 2:31:15time to time, you may come across switch
  3486. 2:31:17statements. It's good to know how they
  3487. 2:31:19work. Before moving on, there's one more
  3488. 2:31:22thing I want to mention. The types don't
  3489. 2:31:24have to match. For example, I'll wrap
  3490. 2:31:27the variable's value with a pair of
  3491. 2:31:29quotes to create a string.
  3492. 2:31:32Believe it or not, our code will work as
  3493. 2:31:35before. The same message gets outputed.
  3494. 2:31:38PHP performs type juggling before
  3495. 2:31:40comparing values. The values do not have
  3496. 2:31:43to be the same type. As long as they
  3497. 2:31:45loosely match, the case will pass.
  3498. 2:31:48That's something you should know when
  3499. 2:31:49working with switch statements. If you
  3500. 2:31:52require strict comparisons, you might
  3501. 2:31:54want to consider using if statements. It
  3502. 2:31:57isn't possible with switch statements.
  3503. 2:31:59In the next lecture, let's look at one
  3504. 2:32:01more feature of performing conditions.
  3505. 2:32:04[Music]
  3506. 2:32:14In this lecture, we're going to explore
  3507. 2:32:16a feature of PHP called match
  3508. 2:32:18expressions. This feature is relatively
  3509. 2:32:21new with version 8 of PHP. If you're on
  3510. 2:32:24an older version of PHP, this feature
  3511. 2:32:26will not be available to you. So, what
  3512. 2:32:29are match expressions? A match
  3513. 2:32:31expression is similar to a switch
  3514. 2:32:33statement. It's a feature that compares
  3515. 2:32:35two values. The main difference is that
  3516. 2:32:37the match keyword is treated as an
  3517. 2:32:40expression. Let me give an example. In
  3518. 2:32:42our script, we added a switch statement.
  3519. 2:32:45Let's wrap the switch statement with the
  3520. 2:32:47variable dump function.
  3521. 2:32:51Almost immediately an error gets thrown
  3522. 2:32:53by PHP. According to the error, the
  3523. 2:32:56switch statement is unexpected. This is
  3524. 2:32:59because the switch statement is not an
  3525. 2:33:01expression. A value does not get
  3526. 2:33:03produced by switch statements.
  3527. 2:33:05Understanding this behavior is crucial.
  3528. 2:33:08Not all lines of code produce values.
  3529. 2:33:10Technically, the switch statement can
  3530. 2:33:12contain expressions, but the switch
  3531. 2:33:14statement itself is not an expression.
  3532. 2:33:17The variable dump function wants an
  3533. 2:33:20expression, otherwise it won't be able
  3534. 2:33:22to output the value into the page. Let's
  3535. 2:33:25switch the keywords from switch to
  3536. 2:33:27match.
  3537. 2:33:30Unlike the switch statement, a match
  3538. 2:33:33expression produces a value. Match
  3539. 2:33:35expressions can be written with the
  3540. 2:33:37match keyword. Inside the parenthesis,
  3541. 2:33:40we may provide a value to compare with
  3542. 2:33:42the values written inside the curly
  3543. 2:33:45brackets. I'm fine sticking with the
  3544. 2:33:47payment status variable. I'll leave it
  3545. 2:33:49as is. Inside the match expression, the
  3546. 2:33:52syntax for adding a list of values is
  3547. 2:33:55slightly different. We don't have to use
  3548. 2:33:57the case keyword. Match expressions use
  3549. 2:34:00a similar syntax to associative arrays.
  3550. 2:34:03For example, we can write the following
  3551. 2:34:05one equals greater than success.
  3552. 2:34:10The value on the left is the value to
  3553. 2:34:12compare with the value passed into the
  3554. 2:34:14match expression. If the values match,
  3555. 2:34:17the value on the right is returned as
  3556. 2:34:19the result of the expression. Multiple
  3557. 2:34:21values can be added by separating them
  3558. 2:34:23with commas. Let's add the number two
  3559. 2:34:26with the following result. Denied.
  3560. 2:34:31Before we test our work, I think it
  3561. 2:34:33would be a good idea to extract the
  3562. 2:34:35expression. It may be difficult to read.
  3563. 2:34:37Values produced by match expressions can
  3564. 2:34:39be stored in a variable. After all, it's
  3565. 2:34:42an expression. I'm going to extract the
  3566. 2:34:44expression to a variable called message.
  3567. 2:34:49Next, I'll pass this variable onto the
  3568. 2:34:52variable dump function.
  3569. 2:34:56After running the script, we were given
  3570. 2:34:58an error. Let's dissect the error to
  3571. 2:35:00understand the problem. The error states
  3572. 2:35:03the following. Uncut unhandled match
  3573. 2:35:06error. unhandled match case. Match
  3574. 2:35:09expressions must always produce a value.
  3575. 2:35:11If we look at our logic, the value
  3576. 2:35:13stored in the variable is a string.
  3577. 2:35:15However, the values in the expressions
  3578. 2:35:17are integers. Unlike switch statements,
  3579. 2:35:20PHP does not perform type juggling on
  3580. 2:35:23any of the values. Comparisons are
  3581. 2:35:25strict. This means that the values must
  3582. 2:35:28match and have matching data types. With
  3583. 2:35:31that in mind, it makes sense why we're
  3584. 2:35:33getting an error. PHP can't find a
  3585. 2:35:35match. In these cases, we must provide a
  3586. 2:35:38default value in the expression. Add the
  3587. 2:35:40default keyword with the following
  3588. 2:35:43value, unknown.
  3589. 2:35:46This time, the error goes away. Instead,
  3590. 2:35:49we're given the default value. That
  3591. 2:35:51makes sense since the data types don't
  3592. 2:35:53match. Let's remove the quotes from the
  3593. 2:35:56variables value.
  3594. 2:35:59After doing so, let's run the script.
  3595. 2:36:02Our
  3596. 2:36:04payment has been denied. Match
  3597. 2:36:06expressions can be easier to read than
  3598. 2:36:08switch and if statements.
  3599. 2:36:12However, you may not like match
  3600. 2:36:14expressions. Here's an example of the
  3601. 2:36:16same result, but with an if statement.
  3602. 2:36:18In programming, there isn't always one
  3603. 2:36:21solution for producing the desired
  3604. 2:36:22results. Different solutions can be used
  3605. 2:36:25to tackle the same problem. Some
  3606. 2:36:27developers may not like match
  3607. 2:36:29expressions. In that case, you can stick
  3608. 2:36:31to if statements. Throughout this
  3609. 2:36:33course, there will be examples of how to
  3610. 2:36:36use features in practical scenarios. For
  3611. 2:36:38now, let's keep moving on with our PHP
  3612. 2:36:41journey. In the next lecture, let's move
  3613. 2:36:43on to functions.
  3614. 2:36:45[Music]
  3615. 2:36:55In this lecture, we're going to start
  3616. 2:36:57writing reusable code by using
  3617. 2:36:59functions. So far, we've been using the
  3618. 2:37:02variable dump function to output data
  3619. 2:37:05into the page. It's a function defined
  3620. 2:37:07by the PHP language. PHP has dozens of
  3621. 2:37:10functions. However, we're not limited to
  3622. 2:37:13PHP's functions. Custom functions can be
  3623. 2:37:16defined by us. Let's look at an example.
  3624. 2:37:18In our script, we've written a chunk of
  3625. 2:37:21code to generate the status of a payment
  3626. 2:37:23based on a number. This solution works,
  3627. 2:37:26but it could be improved further. We may
  3628. 2:37:28want to generate the payment status for
  3629. 2:37:30various places in an application. For
  3630. 2:37:33example, we may want to output this
  3631. 2:37:35information on a dashboard. If the buyer
  3632. 2:37:38wants an invoice, this information could
  3633. 2:37:40be rendered in a PDF file. Lastly, we
  3634. 2:37:43may want to display this information
  3635. 2:37:45after a buyer submits payment
  3636. 2:37:47information. In total, we have three
  3637. 2:37:50different locations where this
  3638. 2:37:52information could appear. As it stands,
  3639. 2:37:55we would have to copy and paste this
  3640. 2:37:57code for the information to be
  3641. 2:37:58displayed. It would be convenient if we
  3642. 2:38:01could write the code once and use it
  3643. 2:38:03anywhere. That's the purpose of a
  3644. 2:38:04function. Functions allow us to write
  3645. 2:38:07logic once. This logic can be repeatedly
  3646. 2:38:10executed anywhere in our codebase. Let's
  3647. 2:38:13try defining a custom function. The goal
  3648. 2:38:16of this function will be to provide the
  3649. 2:38:18status of a payment. Above the current
  3650. 2:38:20code, write the following function get
  3651. 2:38:23status.
  3652. 2:38:26Custom functions can be written by
  3653. 2:38:28typing the function keyword. This
  3654. 2:38:30keyword is followed by the name of the
  3655. 2:38:32function. Function names follow the same
  3656. 2:38:34rules as variable names. Unlike variable
  3657. 2:38:37names, we don't have to start the name
  3658. 2:38:40with a dollar sign character. It's
  3659. 2:38:42common practice to use camel casing for
  3660. 2:38:44function names. After the name, we can
  3661. 2:38:47add a pair of parentheses and curly
  3662. 2:38:49brackets.
  3663. 2:38:52We're going to ignore the parenthesis.
  3664. 2:38:54It's something we'll circle back to.
  3665. 2:38:56Inside the curly brackets, we can write
  3666. 2:38:58the body of the function. Let's move our
  3667. 2:39:00logic into the function.
  3668. 2:39:04Similar to before, we're indenting the
  3669. 2:39:07code for readability. This is always
  3670. 2:39:09true for code written in curly brackets.
  3671. 2:39:12Next, we must invoke the function.
  3672. 2:39:14Invoking a function is another way of
  3673. 2:39:16saying running the code from within the
  3674. 2:39:19body of a function. By default, PHP does
  3675. 2:39:22not execute code inside a function. We
  3676. 2:39:25must explicitly invoke the code. Oftent
  3677. 2:39:28times you may hear developers say call a
  3678. 2:39:30function or run a function. It means the
  3679. 2:39:33same thing. A function can be called by
  3680. 2:39:36typing the name of the function followed
  3681. 2:39:38by a pair of parenthesis. Let's call the
  3682. 2:39:41get status function after defining it.
  3683. 2:39:46After making those changes, the same
  3684. 2:39:48output has been rendered. That's
  3685. 2:39:50perfect. We've successfully defined a
  3686. 2:39:53custom function. This function can be
  3687. 2:39:55called an unlimited number of times.
  3688. 2:39:57There are a few things worth mentioning.
  3689. 2:40:00Firstly, the function can be defined
  3690. 2:40:02anywhere. At the moment, we've defined
  3691. 2:40:05the function first and called it
  3692. 2:40:07afterward. In this instance, the order
  3693. 2:40:09does not matter. We can move the
  3694. 2:40:11function definition below the invocation
  3695. 2:40:14like so.
  3696. 2:40:17The same result occurs. As long as the
  3697. 2:40:19function is defined without
  3698. 2:40:21interference, our code should work. So
  3699. 2:40:24what do I mean by interference? Let's
  3700. 2:40:26wrap our function with an if statement.
  3701. 2:40:32For the condition, use the following
  3702. 2:40:34value. true.
  3703. 2:40:38As you might guess, the code inside the
  3704. 2:40:40conditional statement will always
  3705. 2:40:42execute. However, that doesn't mean the
  3706. 2:40:44function is immediately available like
  3707. 2:40:46before. An undefined error gets produced
  3708. 2:40:49from our script. Functions inside
  3709. 2:40:51conditional statements are not
  3710. 2:40:53immediately available. The conditional
  3711. 2:40:55statement must execute before the
  3712. 2:40:57function is defined. However, we can
  3713. 2:41:00move the conditional statement above the
  3714. 2:41:02invocation to make the code work.
  3715. 2:41:06If we run the script again, things
  3716. 2:41:08should work.
  3717. 2:41:12Everything is back to normal. This can
  3718. 2:41:14be a gotcha with PHP. I always recommend
  3719. 2:41:17defining your functions before executing
  3720. 2:41:19additional logic. By doing so, your
  3721. 2:41:22functions should be readily available.
  3722. 2:41:24It's a common practice among developers.
  3723. 2:41:26I'm going to remove the conditional
  3724. 2:41:28statement wrapped around the function.
  3725. 2:41:33There's one last thing I want to
  3726. 2:41:34mention. Functions cannot be defined
  3727. 2:41:37twice. They're like constants. If we
  3728. 2:41:39attempt to define a function twice, PHP
  3729. 2:41:42will complain. That's something to be
  3730. 2:41:44aware of. In the next lecture, let's
  3731. 2:41:46keep exploring additional features of
  3732. 2:41:49functions.
  3733. 2:41:50[Music]
  3734. 2:42:00In this lecture, we're going to improve
  3735. 2:42:02our function by using parameters. Let's
  3736. 2:42:05take a moment to understand our
  3737. 2:42:07function. While our solution works, it
  3738. 2:42:09can be improved. The biggest issue with
  3739. 2:42:12our function is the message. No matter
  3740. 2:42:14how many times the function is called,
  3741. 2:42:16the same message appears on the page.
  3742. 2:42:19Our function should be flexible. This is
  3743. 2:42:21possible with a feature called
  3744. 2:42:23parameters. A parameter is a variable
  3745. 2:42:26that can be updated outside the
  3746. 2:42:28function. Customizing the variables in a
  3747. 2:42:30function adds flexibility. Inside our
  3748. 2:42:33function, remove the payment status
  3749. 2:42:35variable.
  3750. 2:42:38Next, inside the parentheses of the
  3751. 2:42:40function, add a variable called payment
  3752. 2:42:43status.
  3753. 2:42:46Parameters can be added inside the
  3754. 2:42:48functions parenthesis. Unlike variables,
  3755. 2:42:51values do not need to be assigned. After
  3756. 2:42:54adding this parameter, our script will
  3757. 2:42:56break. In the preview, the error states
  3758. 2:42:59that there are too few arguments. It's
  3759. 2:43:01not the responsibility of the function
  3760. 2:43:03to assign a value to the payment status
  3761. 2:43:06parameter. Instead, the value must be
  3762. 2:43:09provided when the function is called.
  3763. 2:43:11This is why we're getting an error.
  3764. 2:43:13We're not providing a value. A value can
  3765. 2:43:15be sent to the function by writing it in
  3766. 2:43:18parenthesis. Let's pass in one. This
  3767. 2:43:21value will be the value of the payment
  3768. 2:43:24status variable. Let's try testing our
  3769. 2:43:26code by running the script.
  3770. 2:43:30Everything is functioning as before. By
  3771. 2:43:33using parameters, we can avoid hard
  3772. 2:43:35coding a value into the function.
  3773. 2:43:37Functions can be fed data to alter the
  3774. 2:43:40output. We're not limited to a single
  3775. 2:43:42parameter. Multiple parameters can be
  3776. 2:43:44added by separating them with a comma.
  3777. 2:43:47For example, let's add a parameter
  3778. 2:43:49called show message.
  3779. 2:43:53Let's use this parameter to toggle the
  3780. 2:43:55variable dump function. We may not
  3781. 2:43:58always want to output the payment status
  3782. 2:44:00into the console. Wrap the function with
  3783. 2:44:02an if statement. The condition will be
  3784. 2:44:05the show message parameter.
  3785. 2:44:09Next, in the invocation of the get
  3786. 2:44:11status message, let's pass in true.
  3787. 2:44:16The values passed into the function must
  3788. 2:44:19correspond with the parameter list. The
  3789. 2:44:21number will be assigned to the payment
  3790. 2:44:23status parameter. The boolean will be
  3791. 2:44:26assigned to the show message parameter.
  3792. 2:44:28If we reverse these values, the values
  3793. 2:44:31for the parameters will change. The
  3794. 2:44:33order does matter. In some cases, you
  3795. 2:44:36may want to assign default values to
  3796. 2:44:38your parameters. What if we had five
  3797. 2:44:41parameters? Constantly passing in five
  3798. 2:44:44values to a function can be annoying.
  3799. 2:44:46It's optional to add default values for
  3800. 2:44:48a parameter. We can do so from the
  3801. 2:44:51parameter list. Let's set the show
  3802. 2:44:54message parameter to true.
  3803. 2:44:58Next, we can remove the true value from
  3804. 2:45:01the invocation.
  3805. 2:45:04PHP won't throw an error if we don't
  3806. 2:45:07pass on a value for a specific
  3807. 2:45:09parameter. PHP checks the parameter for
  3808. 2:45:12a default value. If there is a default
  3809. 2:45:14value, it'll be used. Values passed into
  3810. 2:45:17a function will override the default
  3811. 2:45:19value of a parameter. We're getting the
  3812. 2:45:22same output as before. That's awesome.
  3813. 2:45:24There's one last thing I want to mention
  3814. 2:45:26before moving on. In the programming
  3815. 2:45:28world, you're going to hear two words
  3816. 2:45:31called parameters and arguments. Often
  3817. 2:45:34times, developers use these words
  3818. 2:45:36interchangeably, but there's a
  3819. 2:45:38difference. A parameter refers to the
  3820. 2:45:41variable in the functions parameter
  3821. 2:45:43list. Whereas an argument refers to the
  3822. 2:45:46value of the parameter. In our case, the
  3823. 2:45:48payment status and show message
  3824. 2:45:51variables are parameters. No one's going
  3825. 2:45:54to yell at you for using the wrong term.
  3826. 2:45:56However, it's always good to know the
  3827. 2:45:58difference. Throughout the course,
  3828. 2:46:00you'll see me using both terms to
  3829. 2:46:02describe a functions parameters. In the
  3830. 2:46:04next lecture, let's continue improving
  3831. 2:46:06our function by exploring return values.
  3832. 2:46:10[Music]
  3833. 2:46:20In this lecture, we're going to expose
  3834. 2:46:22data from a function to the outside
  3835. 2:46:24world. The get status function takes in
  3836. 2:46:27a value. Based on this value, it'll
  3837. 2:46:29produce a readable message for the
  3838. 2:46:31payment status. This information is not
  3839. 2:46:33available outside of the function.
  3840. 2:46:35Exposing information outside of a
  3841. 2:46:38function is a common practice for
  3842. 2:46:39developers. What if we want to change
  3843. 2:46:42the language of the text? Alternatively,
  3844. 2:46:44we may want to change the formatting by
  3845. 2:46:47wrapping the text with HTML tags. Not
  3846. 2:46:50all pages may render text the same. For
  3847. 2:46:52the most flexibility, we should expose
  3848. 2:46:55the payment status to outside sources.
  3849. 2:46:58This behavior is possible by returning
  3850. 2:47:00the value. At the bottom of the
  3851. 2:47:02function, add the return keyword
  3852. 2:47:04followed by the message variable.
  3853. 2:47:08By returning the value from the
  3854. 2:47:10function, a few things happen. Firstly,
  3855. 2:47:13the code inside the function stops
  3856. 2:47:15executing. If we were to continue
  3857. 2:47:17writing code after the return statement,
  3858. 2:47:20nothing would happen. PHP won't execute
  3859. 2:47:23the code written after this statement.
  3860. 2:47:25This is why we've written the statement
  3861. 2:47:27at the end of the function. Now the
  3862. 2:47:29question is how do we access this
  3863. 2:47:31information? Returning a value from a
  3864. 2:47:34function can be stored when it's called.
  3865. 2:47:36Below our function, we're invoking the
  3866. 2:47:38get status function. By returning a
  3867. 2:47:40value, we can store this information in
  3868. 2:47:43a variable. Let's set a variable called
  3869. 2:47:46status message to the functions return
  3870. 2:47:48value.
  3871. 2:47:51Next, call the variable dump function
  3872. 2:47:54with the status message variable.
  3873. 2:47:58The message gets rendered twice, once
  3874. 2:48:01from inside the function and another
  3875. 2:48:03time from outside the function.
  3876. 2:48:05Returning data is optional, but it can
  3877. 2:48:07be useful to use data from a function
  3878. 2:48:09after it's been executed. Not all
  3879. 2:48:12functions need to return data. It's
  3880. 2:48:14completely optional. The variable dump
  3881. 2:48:16function is an example of a function
  3882. 2:48:19that doesn't return data. Overall,
  3883. 2:48:22functions are a powerful feature for
  3884. 2:48:24reusing logic. In a few lectures, we've
  3885. 2:48:27created a function that can convert a
  3886. 2:48:29status from a number to a human readable
  3887. 2:48:31message. It's very flexible and gives us
  3888. 2:48:34the power to customize its behavior.
  3889. 2:48:37Functions have so much more to offer.
  3890. 2:48:39For the next set of lectures, we're
  3891. 2:48:41going to explore these features. I'll
  3892. 2:48:43see you in the next one.
  3893. 2:48:45[Music]
  3894. 2:48:55In this lecture, we're going to apply
  3895. 2:48:57type hinting to our function. At the
  3896. 2:49:00moment, our function returns a string.
  3897. 2:49:02PHP gives us the flexibility to return
  3898. 2:49:05any type of value. It doesn't know the
  3899. 2:49:07return type of our functions. It guesses
  3900. 2:49:09the type during runtime. It's entirely
  3901. 2:49:12possible to inform PHP of the return
  3902. 2:49:15type of our function. There are many
  3903. 2:49:17benefits to doing so. The main advantage
  3904. 2:49:19is debugging an application. We've
  3905. 2:49:22talked about this before. PHP is a
  3906. 2:49:24dynamically typed language. It's capable
  3907. 2:49:27of changing the data type of a variable
  3908. 2:49:29at a moment's notice. We do not need to
  3909. 2:49:32set the types of our variables. While
  3910. 2:49:34convenient, that doesn't mean there
  3911. 2:49:36aren't drawbacks. It's possible to use
  3912. 2:49:38the wrong data type for a specific
  3913. 2:49:40situation. Rather than deal with the
  3914. 2:49:43headaches of a dynamically typed
  3915. 2:49:44language, we can enforce types. By doing
  3916. 2:49:47so, if we attempt to use a type
  3917. 2:49:49incompatible with our code, we can be
  3918. 2:49:52informed of the problem before testing
  3919. 2:49:53our code. It's an incredibly valuable
  3920. 2:49:56feature that'll save us hours of
  3921. 2:49:58debugging. Return types can be enforced
  3922. 2:50:00by adding a colon character after the
  3923. 2:50:03parenthesis. After this character, we
  3924. 2:50:05can specify a type. Let's use the string
  3925. 2:50:08type.
  3926. 2:50:11Adding this type can help PHP understand
  3927. 2:50:14the type of value returned by the
  3928. 2:50:16function. In some cases, we may not
  3929. 2:50:18always return a type. For example, in
  3930. 2:50:21our function, we're conditionally
  3931. 2:50:23dumping the message. I don't think we
  3932. 2:50:25should return the message from the
  3933. 2:50:26function if the show message parameter
  3934. 2:50:29is set to true. At the end of the
  3935. 2:50:31conditional statement, let's return
  3936. 2:50:33null.
  3937. 2:50:36As you can imagine, returning a value
  3938. 2:50:38causes the message to never be returned.
  3939. 2:50:41This will only occur if the show message
  3940. 2:50:43parameter is true. Returning a null will
  3941. 2:50:46break our function. It's because we're
  3942. 2:50:49returning null instead of a string. If
  3943. 2:50:51we didn't have type hinting in our
  3944. 2:50:53function, everything would be fine. In
  3945. 2:50:55these cases, we can update our function
  3946. 2:50:58by adding a question mark character
  3947. 2:51:00before the return type.
  3948. 2:51:04Adding this character allows a null
  3949. 2:51:06value to be returned by the function.
  3950. 2:51:08Thus, our function can return a string
  3951. 2:51:11or null. In some cases, your function
  3952. 2:51:14may not return anything at all. It's
  3953. 2:51:16optional to return a value. If your
  3954. 2:51:18function doesn't return a value, you can
  3955. 2:51:21set the return type to void.
  3956. 2:51:25The void data type is a special data
  3957. 2:51:28type for functions. It was introduced
  3958. 2:51:30for functions that never return data. In
  3959. 2:51:33our case, we don't need this type.
  3960. 2:51:35Regardless, it's good to know. Type
  3961. 2:51:38hinting is not just available for return
  3962. 2:51:40types. We can add type hinting to
  3963. 2:51:43parameters. For example, let's add the
  3964. 2:51:45integer type to the payment status
  3965. 2:51:48parameter.
  3966. 2:51:51Types must be added before the parameter
  3967. 2:51:53name. Let's add the boolean type to the
  3968. 2:51:56second parameter. We're not limited to a
  3969. 2:51:59single type. PHP supports union types.
  3970. 2:52:02Unions types are for variables that
  3971. 2:52:04support different data types. For
  3972. 2:52:07example, the status variable accepts a
  3973. 2:52:10number. What if we want to accept the
  3974. 2:52:12float data type? It's not uncommon to
  3975. 2:52:15support integers and floats. An
  3976. 2:52:17additional type can be supported by
  3977. 2:52:19adding the pipe character followed by
  3978. 2:52:21the new type. Let's add the float type.
  3979. 2:52:26We can add as many types as we'd like.
  3980. 2:52:29Alternatively, you can use a type called
  3981. 2:52:31mixed.
  3982. 2:52:34You may have parameters that accept all
  3983. 2:52:37data types. Rather than listing each
  3984. 2:52:39possible data type, you can use the
  3985. 2:52:41mixed data type as a shortcut. This data
  3986. 2:52:44type was designed to accept all data
  3987. 2:52:47types for a parameter for our function.
  3988. 2:52:49It's not necessary. I'll revert it to
  3989. 2:52:52the previous union type.
  3990. 2:52:56Type hinting is the power to explicitly
  3991. 2:52:58set the data type of our parameters and
  3992. 2:53:01return values. Enforcing data types
  3993. 2:53:03reduces the likelihood of errors.
  3994. 2:53:06However, we're not fully benefiting from
  3995. 2:53:08type hinting. In the next lecture, let's
  3996. 2:53:10discuss why this is the case.
  3997. 2:53:13[Music]
  3998. 2:53:23In this lecture, we're going to enable
  3999. 2:53:25strict typing for our files. At the
  4000. 2:53:28moment, our function has type hinting.
  4001. 2:53:30However, type hinting isn't enforced.
  4002. 2:53:33For example, I'm going to update the
  4003. 2:53:35argument passed into the get status
  4004. 2:53:37function by wrapping it with quotes.
  4005. 2:53:42After doing so, nothing happens in the
  4006. 2:53:44preview. Everything is perfectly
  4007. 2:53:46functional. Things become more bizarre
  4008. 2:53:48when we look at the results. As we know,
  4009. 2:53:51match expressions compare values based
  4010. 2:53:53on the value and data type. In our
  4011. 2:53:56expression, we're using integers, but
  4012. 2:53:58we're passing in a string. Regardless,
  4013. 2:54:00it was able to find a match. This
  4014. 2:54:02behavior occurs because PHP is
  4015. 2:54:05performing type juggling. By adding type
  4016. 2:54:07hinting, PHP casts the argument into the
  4017. 2:54:11correct type before the block of code is
  4018. 2:54:13executed. Therefore, our parameter is
  4019. 2:54:15going to have the correct type. That's
  4020. 2:54:18not exactly what we want. We shouldn't
  4021. 2:54:20be passing in the wrong data type to
  4022. 2:54:22begin with. A better approach would be
  4023. 2:54:24to receive an error from PHP. This way,
  4024. 2:54:27we can adjust the data type. Type
  4025. 2:54:29juggling would be skipped entirely. PHP
  4026. 2:54:32is more than capable of detecting types.
  4027. 2:54:35However, this feature is not enabled. We
  4028. 2:54:37must manually enable it from the top of
  4029. 2:54:40the file. At the top of the file, add
  4030. 2:54:42the following. Declare strict types
  4031. 2:54:45equals 1.
  4032. 2:54:48The declare keyword allows us to
  4033. 2:54:50configure PHP's behavior. In this
  4034. 2:54:53example, we're enabling the strict types
  4035. 2:54:56option by setting it to one. After
  4036. 2:54:58enabling this feature, the page throws
  4037. 2:55:00an error. If I look at the error, the
  4038. 2:55:03error states I'm using the string type
  4039. 2:55:06when the function accepts a value with
  4040. 2:55:08the integer or float type. PHP is not
  4041. 2:55:11allowing our script to execute normally.
  4042. 2:55:14Enabling strict mode prevents our script
  4043. 2:55:16from passing in a value with the wrong
  4044. 2:55:18data type. If we were to remove the
  4045. 2:55:21quotes, the error should go away.
  4046. 2:55:25By enabling strict mode, we're
  4047. 2:55:27guaranteed to always be working with the
  4048. 2:55:30correct data type. Weakly typed
  4049. 2:55:32languages can introduce unintended
  4050. 2:55:34behavior in your application. These
  4051. 2:55:36types of bugs can be hard to fix. I
  4052. 2:55:39always recommend enabling strict typing
  4053. 2:55:41to avoid bugs. There is a catch. The
  4054. 2:55:44declare statement is only applicable to
  4055. 2:55:46the current file. If we had another PHP
  4056. 2:55:49file, we must add the same statement to
  4057. 2:55:52the file. Otherwise, strict mode won't
  4058. 2:55:55be enabled. It can be annoying to type
  4059. 2:55:57the same line of code, but it is worth
  4060. 2:55:59the effort. In the next lecture, let's
  4061. 2:56:02continue our PHP journey.
  4062. 2:56:04[Music]
  4063. 2:56:14In this lecture, we're going to discuss
  4064. 2:56:16a small gotcha with logical operators. I
  4065. 2:56:19know this might seem strange because we
  4066. 2:56:21spent a lot of time talking about
  4067. 2:56:23operators from the previous section.
  4068. 2:56:25However, we couldn't discuss this
  4069. 2:56:27feature until we talked about functions.
  4070. 2:56:30Let's circle back to operators just for
  4071. 2:56:32this lecture. PHP is lazy when it comes
  4072. 2:56:35to conditional expressions. Believe it
  4073. 2:56:37or not, it's completely possible for PHP
  4074. 2:56:40to skip an expression when evaluating
  4075. 2:56:42conditions with logical operators. Let
  4076. 2:56:45me show you what I mean. In the PHP
  4077. 2:56:47file, clear the contents of the file.
  4078. 2:56:52Next, we're going to call the variable
  4079. 2:56:54dump function with the following
  4080. 2:56:56expression, true and true.
  4081. 2:57:01PHP executes the expression in the order
  4082. 2:57:04of the conditions. Firstly, it'll check
  4083. 2:57:07if the first condition evaluates to
  4084. 2:57:09true. If it does, the second expression
  4085. 2:57:12is checked. Nothing complicated so far.
  4086. 2:57:14But what if the first expression
  4087. 2:57:16evaluates to false?
  4088. 2:57:20In this case, it doesn't make sense for
  4089. 2:57:22PHP to check the second condition. The
  4090. 2:57:25entire expression will evaluate as false
  4091. 2:57:27regardless of the value from the second
  4092. 2:57:30condition. Both conditions must be true
  4093. 2:57:32for the entire expression to return
  4094. 2:57:34true. This behavior can potentially
  4095. 2:57:37cause a problem. Define a custom
  4096. 2:57:39function called example.
  4097. 2:57:43In this function, let's echo a message
  4098. 2:57:45to indicate the function called.
  4099. 2:57:49Next, let's return true.
  4100. 2:57:54Lastly, let's replace the second
  4101. 2:57:56condition with an invocation of the
  4102. 2:57:58example function.
  4103. 2:58:01What do you think will happen? Do you
  4104. 2:58:03think the message from the example
  4105. 2:58:05function will appear on the page? Let's
  4106. 2:58:07find out.
  4107. 2:58:10Inside the page, only the variable has
  4108. 2:58:13been logged. A message never gets echoed
  4109. 2:58:15from our function. This is because PHP
  4110. 2:58:18does not bother executing our function.
  4111. 2:58:20Since the first condition was false, the
  4112. 2:58:23entire expression was false. This
  4113. 2:58:25behavior is beneficial. It prevents PHP
  4114. 2:58:28from executing unnecessary logic.
  4115. 2:58:31Applications should always be optimized
  4116. 2:58:33for performance. This feature is known
  4117. 2:58:35as shortcircuiting, which is common in
  4118. 2:58:38most programming languages. Let's try
  4119. 2:58:40updating the first condition to true.
  4120. 2:58:45As you can see, the message from our
  4121. 2:58:47function has been echoed. You should
  4122. 2:58:49always be aware of this feature. If you
  4123. 2:58:51have a function that's expensive to run,
  4124. 2:58:53you may want to consider adding it to
  4125. 2:58:55the end of a condition. This way, you
  4126. 2:58:57don't waste precious resources. That's
  4127. 2:59:00all I wanted to mention for operators.
  4128. 2:59:02In the next lecture, let's get back into
  4129. 2:59:05controlling logic flow.
  4130. 2:59:07[Music]
  4131. 2:59:18In this lecture, we're going to start
  4132. 2:59:20exploring loops. Looping is the process
  4133. 2:59:22of repeating the same actions. A loop
  4134. 2:59:24can be performed with conditions or a
  4135. 2:59:27collection of values. We're going to
  4136. 2:59:29explore both options. To get started,
  4137. 2:59:31we'll perform a loop based on a
  4138. 2:59:33condition. In the script, I'm going to
  4139. 2:59:35remove all the code.
  4140. 2:59:39Next, let's create a variable called a
  4141. 2:59:42with an initial value of one.
  4142. 2:59:46This lecture will aim to output the
  4143. 2:59:48numbers 1 through 15. This variable will
  4144. 2:59:51be updated with each number. As we do
  4145. 2:59:53so, we'll output the number into the
  4146. 2:59:55page. This is the perfect use case for a
  4147. 2:59:58while loop. A while loop allows us to
  4148. 3:00:00repeat actions based on a condition.
  4149. 3:00:03Let's look at an example. Below the
  4150. 3:00:05variable, add the while keyword with a
  4151. 3:00:08pair of parentheses and curly brackets.
  4152. 3:00:13Inside the parenthesis, we must provide
  4153. 3:00:15an expression that evaluates to a
  4154. 3:00:17boolean value. If we don't provide a
  4155. 3:00:20boolean value, PHP type juggles the
  4156. 3:00:23value into a boolean. Expressions that
  4157. 3:00:25evaluate to true will cause the code
  4158. 3:00:27inside the curly brackets to execute.
  4159. 3:00:30Otherwise, nothing happens. Let's use
  4160. 3:00:32the following condition. A less than
  4161. 3:00:35equals 15.
  4162. 3:00:39In this example, the A variable must be
  4163. 3:00:42less than or equal to the number 15.
  4164. 3:00:44Since our variable stores the number
  4165. 3:00:46one, this condition evaluates to true.
  4166. 3:00:49Thus, our loop will be executed. PHP
  4167. 3:00:52always checks the condition before
  4168. 3:00:54executing the block of code. Inside our
  4169. 3:00:57block of code, let's write the
  4170. 3:00:59following. Echo A.Br.
  4171. 3:01:04In this example, we're echoing the
  4172. 3:01:06number along with the break tag. This is
  4173. 3:01:08so that our numbers aren't cluttered.
  4174. 3:01:10We're almost finished. At the moment, we
  4175. 3:01:13have an infinite loop. Infinite loops
  4176. 3:01:15are considered to be highly dangerous.
  4177. 3:01:17After a block of code has been executed,
  4178. 3:01:20PHP evaluates the expression written
  4179. 3:01:22from within the parenthesis. Again, if
  4180. 3:01:24the expression evaluates to true, the
  4181. 3:01:27block of code is executed once more.
  4182. 3:01:29This process repeats until the condition
  4183. 3:01:32evaluates to false. A false value causes
  4184. 3:01:35PHP to move on to the next line of code
  4185. 3:01:38written after the loop. Our condition
  4186. 3:01:41always evaluates to true. We are never
  4187. 3:01:43updating the A variable. It's always
  4188. 3:01:46considered good practice to avoid
  4189. 3:01:48infinite loops. Otherwise, a block of
  4190. 3:01:50code runs endlessly. Resources are
  4191. 3:01:53consumed to the point that a system will
  4192. 3:01:55crash. After echoing the value, let's
  4193. 3:01:58write the following. A++.
  4194. 3:02:02We could have used the addition operator
  4195. 3:02:05to accomplish this task. However, it's
  4196. 3:02:07not uncommon to increment a value by one
  4197. 3:02:10from within a loop. For this reason, PHP
  4198. 3:02:13introduces the increment operator to add
  4199. 3:02:16one to a value. It's cleaner to read
  4200. 3:02:18than the other solution. On each
  4201. 3:02:20iteration, the number will increment.
  4202. 3:02:23Eventually, the variable will be greater
  4203. 3:02:25than 15. Thus, our loop stops running.
  4204. 3:02:28The numbers 1 through 15 appear on the
  4205. 3:02:31page. The while loop is a powerful
  4206. 3:02:33feature. PHP has an alternative syntax
  4207. 3:02:36for writing while loops called dowh.
  4208. 3:02:39Let's convert our while loop into a
  4209. 3:02:41dowhile loop. First, move the while
  4210. 3:02:44portion of the code from the beginning
  4211. 3:02:46to the end of the loop.
  4212. 3:02:50Next, add a semicolon.
  4213. 3:02:54Lastly, add the do keyword at the
  4214. 3:02:56beginning of the loop.
  4215. 3:02:59There's one major difference between a
  4216. 3:03:01while and dowhile loop. A dowhile loop
  4217. 3:03:04executes code from within the curly
  4218. 3:03:06brackets at least once. This section of
  4219. 3:03:09code is guaranteed to run once. After
  4220. 3:03:11executing the block of code, PHP checks
  4221. 3:03:14the conditions. Previously, the while
  4222. 3:03:17loop would perform the condition check
  4223. 3:03:19first. It's uncommon to use the dowhile
  4224. 3:03:21loop, but it can be helpful when you
  4225. 3:03:23need to run code at least once. In
  4226. 3:03:25addition, a semicolon is necessary for
  4227. 3:03:28the while keyword. Typically, PHP does
  4228. 3:03:31not require the semicolon for blocks of
  4229. 3:03:33code. However, since we're writing
  4230. 3:03:35additional code after the closing
  4231. 3:03:37bracket, we must manually end the
  4232. 3:03:39statement. The same results are outputed
  4233. 3:03:42into the page. That's to be expected
  4234. 3:03:44since the condition doesn't change. To
  4235. 3:03:46prove the block of code executes, let's
  4236. 3:03:49change the condition by setting the
  4237. 3:03:51number to zero.
  4238. 3:03:54If we run the script again, only a
  4239. 3:03:56single number appears despite the
  4240. 3:03:58condition evaluating to false. PHP has
  4241. 3:04:02executed our block of code before
  4242. 3:04:04checking the condition. In most cases,
  4243. 3:04:06you probably won't need the dowhile
  4244. 3:04:08loop, but it is nice to know. In the
  4245. 3:04:10next lecture, let's keep exploring
  4246. 3:04:12loops.
  4247. 3:04:14[Music]
  4248. 3:04:25In this lecture, we're going to explore
  4249. 3:04:27the for loop. The for loop was designed
  4250. 3:04:30for repeating actions based on a
  4251. 3:04:32condition. The code is going to be
  4252. 3:04:33similar to the while loop. A few parts
  4253. 3:04:36are going to be in different locations.
  4254. 3:04:38Let's look at an example. In the PHP
  4255. 3:04:41file, replace the contents of the file
  4256. 3:04:43with the for keyword. This keyword is
  4257. 3:04:46followed by a pair of parentheses and
  4258. 3:04:48curly brackets.
  4259. 3:04:51Inside the for keyword, we must provide
  4260. 3:04:54three pieces of information. First, we
  4261. 3:04:56must initialize a variable. Let's create
  4262. 3:04:59a variable called I with an initial
  4263. 3:05:01value of one.
  4264. 3:05:04Previously, the while loop accepted a
  4265. 3:05:07condition. Other pieces of information
  4266. 3:05:09had to be managed in external areas. The
  4267. 3:05:12for loop is different. Instead of being
  4268. 3:05:14able to freely define variables outside
  4269. 3:05:16of the loop, they can be written within
  4270. 3:05:18the parenthesis. This adds more clarity
  4271. 3:05:21to our loop. The second piece of
  4272. 3:05:23information required by the loop is the
  4273. 3:05:25condition. Let's use the same condition
  4274. 3:05:27as before, which was the following. I
  4275. 3:05:30less than equals 15.
  4276. 3:05:34Lastly, we must provide an expression to
  4277. 3:05:37increment the i variable. Let's use the
  4278. 3:05:39increment operator.
  4279. 3:05:43The syntax may feel strange. When PHP
  4280. 3:05:46encounters a for loop, it'll execute the
  4281. 3:05:48first portion of the loop, which is
  4282. 3:05:50initializing a variable. The variable is
  4283. 3:05:53never initialized again. It doesn't
  4284. 3:05:55matter if the loop runs several times.
  4285. 3:05:58This bit of code runs once. Next, PHP
  4286. 3:06:02executes the second piece of code. If
  4287. 3:06:04the expression evaluates to true, the
  4288. 3:06:06block of code gets executed. Inside the
  4289. 3:06:09block of code, let's echo the I variable
  4290. 3:06:12with a break element.
  4291. 3:06:15After the block of code has been
  4292. 3:06:17executed, PHP executes the expression
  4293. 3:06:20from the third section of the loop.
  4294. 3:06:22After doing so, it'll execute the second
  4295. 3:06:25condition. This process repeats until
  4296. 3:06:27the condition evaluates to false.
  4297. 3:06:30Essentially, we have the same thing as
  4298. 3:06:32before. The main difference is that the
  4299. 3:06:34logic has been centralized into a single
  4300. 3:06:37location which may be easier to read on
  4301. 3:06:40the page. The numbers 1 through 15 have
  4302. 3:06:42been rendered. That's great. So why do
  4303. 3:06:45we have two loops for performing the
  4304. 3:06:47same thing? When would we use a while
  4305. 3:06:50loop over a for loop? Vice versa, why
  4306. 3:06:53would we use a for loop over a while
  4307. 3:06:55loop? The for loop was introduced
  4308. 3:06:58performing a loop based on a specific
  4309. 3:07:00number. If you want to perform a loop 10
  4310. 3:07:02times, you can tailor the expressions to
  4311. 3:07:04this scenario. Whereas while loops are
  4312. 3:07:07not restricted to a count, in some
  4313. 3:07:09cases, we may want to perform a loop
  4314. 3:07:11based on strings, arrays, or other types
  4315. 3:07:14of values. We're not restricted to
  4316. 3:07:16numbers. It's completely possible to
  4317. 3:07:18stick to one loop. You don't have to use
  4318. 3:07:21both loops in your programs. Most
  4319. 3:07:23developers prefer the while loop. It's
  4320. 3:07:26simpler than a for loop. Before moving
  4321. 3:07:28on, there's one more thing I want to
  4322. 3:07:30mention. PHP gives us the power to
  4323. 3:07:32control loops with additional keywords.
  4324. 3:07:35For example, we may want to skip a
  4325. 3:07:37number. Inside the for loop, let's add
  4326. 3:07:40an if statement.
  4327. 3:07:43The condition will be the following. i=
  4328. 3:07:476.
  4329. 3:07:50It's perfectly valid to nest blocks of
  4330. 3:07:53code. In this case, we're performing a
  4331. 3:07:55condition before echoing the value. The
  4332. 3:07:57equals equals equals operator is a
  4333. 3:08:00comparison operator. It'll check if the
  4334. 3:08:02values are equal to each other. If they
  4335. 3:08:05are, we should skip the current
  4336. 3:08:06iteration of the loop by using the
  4337. 3:08:09continue keyword.
  4338. 3:08:12The continue keyword can be used in any
  4339. 3:08:14loop. This keyword instructs PHP to stop
  4340. 3:08:18executing the block of code. It should
  4341. 3:08:20immediately recheck the condition. You
  4342. 3:08:22don't have to wait for the entire block
  4343. 3:08:24of code to run. If you need to stop the
  4344. 3:08:27current iteration from running, you can
  4345. 3:08:29do so with the continue keyword. As you
  4346. 3:08:31can see, the number six has been
  4347. 3:08:33skipped. It does not appear in the
  4348. 3:08:35output. Numbers after six appear. The
  4349. 3:08:38continue keyword does not completely
  4350. 3:08:40stop the loop. What if you want to end
  4351. 3:08:42the loop completely? That's possible by
  4352. 3:08:45replacing the continue keyword with the
  4353. 3:08:48break keyword.
  4354. 3:08:51By using the break keyword, the loop can
  4355. 3:08:53be stopped earlier than intended. These
  4356. 3:08:56keywords are not exclusive to the for
  4357. 3:08:58loop. You can use them for any loop. In
  4358. 3:09:00the next lecture, let's explore one more
  4359. 3:09:03type of loop.
  4360. 3:09:04[Music]
  4361. 3:09:16In this lecture, we're going to explore
  4362. 3:09:18one more feature for loops called a for
  4363. 3:09:21each loop. So far, we've been performing
  4364. 3:09:23a loop based on a condition. We're not
  4365. 3:09:25limited to conditions. In some cases, we
  4366. 3:09:28may need to loop through an array. It's
  4367. 3:09:30a very common practice. PHP offers the
  4368. 3:09:33for each loop for this specific purpose.
  4369. 3:09:36Let's give it a try. First, let's create
  4370. 3:09:38an array. In the script, clear the
  4371. 3:09:41contents of the file.
  4372. 3:09:44Next, define an array called names.
  4373. 3:09:49Inside this array, add a list of random
  4374. 3:09:51names.
  4375. 3:09:54Lastly, add the for each keyword with a
  4376. 3:09:57pair of parentheses and curly brackets.
  4377. 3:10:02Our goal is to loop through each item in
  4378. 3:10:04the array. During each iteration, we'll
  4379. 3:10:06output the name. The for each keyword
  4380. 3:10:09accepts a specific format. Type the
  4381. 3:10:12following names as name.
  4382. 3:10:16First, we must provide the array.
  4383. 3:10:19Afterward, we can use the as keyword to
  4384. 3:10:21create a reference for each item in the
  4385. 3:10:23array. As PHP loops through the items in
  4386. 3:10:26the array, the current item in the
  4387. 3:10:28iteration will be stored in the variable
  4388. 3:10:30after the as keyword. It's common
  4389. 3:10:33practice to use the plural version of a
  4390. 3:10:35word when defining arrays. Items in an
  4391. 3:10:38array use the singular form of a word.
  4392. 3:10:40You don't have to follow this practice,
  4393. 3:10:42but it's commonly adopted among
  4394. 3:10:44developers. PHP handles going through
  4395. 3:10:47each item in the array. Inside the loop,
  4396. 3:10:50let's call the variable dump function
  4397. 3:10:52with the name variable.
  4398. 3:10:56All three items from the array are
  4399. 3:10:58listed. PHP goes through each item from
  4400. 3:11:01first to last. Overall, the for each
  4401. 3:11:04loop makes it easy to loop through items
  4402. 3:11:06in the array. There are a few things I
  4403. 3:11:09want to mention. Sometimes you may want
  4404. 3:11:11to grab the key of the item in the
  4405. 3:11:13array. It's completely optional to grab
  4406. 3:11:16this information from the current
  4407. 3:11:17iteration. In the parentheses, let's
  4408. 3:11:20update the expression by adding the
  4409. 3:11:22following after the as keyword. Key
  4410. 3:11:25equals greater than.
  4411. 3:11:28The syntax is similar to associative
  4412. 3:11:31arrays. To the left of the arrow, we
  4413. 3:11:33must provide a variable for storing the
  4414. 3:11:36key of the item. You don't have to use
  4415. 3:11:38these variable names. You can use
  4416. 3:11:40whatever name you'd like. Generally,
  4417. 3:11:42developers will use the word key for
  4418. 3:11:44clarity. Inside our function, call the
  4419. 3:11:47variable dump function with the key
  4420. 3:11:50variable.
  4421. 3:11:52Looking at the preview, the key is
  4422. 3:11:55accessible via the variable. This
  4423. 3:11:57concludes our discussion on loops. So
  4424. 3:12:00far, we've explored four features for
  4425. 3:12:02writing loops. PHP offers us the power
  4426. 3:12:05to loop through an array or to write a
  4427. 3:12:07condition to power the loop. Hopefully,
  4428. 3:12:10it's clear why you would use each loop.
  4429. 3:12:13[Music]
  4430. 3:12:25In this lecture, we will explore a few
  4431. 3:12:28of PHP's predefined constants. They may
  4432. 3:12:31come in handy from time to time. In the
  4433. 3:12:33resource section of this lecture, I
  4434. 3:12:35provide a link to an official list of
  4435. 3:12:37predefined constants. These constants
  4436. 3:12:40are defined by the PHP language. You
  4437. 3:12:43don't have to do anything to include
  4438. 3:12:44them in your project. They are readily
  4439. 3:12:46accessible. Let's go over some of them.
  4440. 3:12:49Before we start using these constants,
  4441. 3:12:51I'm going to clear the contents of the
  4442. 3:12:53PHP file except for the opening PHP tag.
  4443. 3:12:58All right, let's echo out a constant
  4444. 3:13:01called PHP version.
  4445. 3:13:04As you might suspect, this constant
  4446. 3:13:07outputs the current PHP version on your
  4447. 3:13:09server. This constant might come in
  4448. 3:13:12handy if you're developing a script that
  4449. 3:13:14will be deployed on multiple servers.
  4450. 3:13:16Verifying the PHP version is a common
  4451. 3:13:19action taken amongst PHP developers. If
  4452. 3:13:22a server has an outdated PHP version,
  4453. 3:13:25you can use this opportunity to notify
  4454. 3:13:27an administrator to update the version.
  4455. 3:13:30Older versions of PHP may have security
  4456. 3:13:32bugs or flaws. In addition, you may be
  4457. 3:13:35using a feature in PHP unavailable to
  4458. 3:13:38previous versions. As you can see from
  4459. 3:13:40the preview, I'm using PHP8. You may
  4460. 3:13:43have a newer version depending on when
  4461. 3:13:45you're watching this video. All right,
  4462. 3:13:47let's try echoing a different constant
  4463. 3:13:50called PHP int max.
  4464. 3:13:55I'll concatenate a break element to echo
  4465. 3:13:58another constant called PHP ins min.
  4466. 3:14:03Technically, there's an infinite set of
  4467. 3:14:05numbers. However, that doesn't mean that
  4468. 3:14:08most computers are able to handle those
  4469. 3:14:10numbers. In most cases, there's a
  4470. 3:14:12maximum and minimum set of numbers that
  4471. 3:14:14PHP can handle. Luckily, the range of
  4472. 3:14:17numbers is extremely vast. It's uncommon
  4473. 3:14:20to exceed these thresholds. If you're
  4474. 3:14:22ever curious as to what those thresholds
  4475. 3:14:24are, PHP stores the minimum and maximum
  4476. 3:14:28energy values in those constants. The
  4477. 3:14:30same goes for float values. For example,
  4478. 3:14:33I'll replace the word integer with
  4479. 3:14:35float.
  4480. 3:14:38If we look at the preview, the format of
  4481. 3:14:40the numbers may appear strange. There's
  4482. 3:14:42a letter E in the middle of the number.
  4483. 3:14:45PHP uses scientific notation for numbers
  4484. 3:14:48that are too large to display. It's not
  4485. 3:14:51something you'll ever have to worry
  4486. 3:14:52about since most developers never exceed
  4487. 3:14:55these limits. However, they're still
  4488. 3:14:57good to know just in case. All right,
  4489. 3:15:00those are some of the simpler constants
  4490. 3:15:02offered by PHP. There's another subset
  4491. 3:15:05of constants that are bizarre to say the
  4492. 3:15:07least. In the resource section of this
  4493. 3:15:09lecture, I provide a link to a page of
  4494. 3:15:12magic constants. Magic constants defy
  4495. 3:15:15the very concept of a constant. Unlike
  4496. 3:15:18most constants, the value of a magic
  4497. 3:15:20constant can change. Out of the box, PHP
  4498. 3:15:23offers nine magic constants. So, what
  4499. 3:15:26are they and why can they change? To
  4500. 3:15:29understand how magic constants work,
  4501. 3:15:31let's try using one. Back in our
  4502. 3:15:33project, let's echo a constant called
  4503. 3:15:36underscore
  4504. 3:15:38line_.
  4505. 3:15:42So, one thing worth noting about magic
  4506. 3:15:45constants is that they all start and end
  4507. 3:15:47with two underscores. This naming
  4508. 3:15:49convention is how you can identify a
  4509. 3:15:51magic constant. The line constant
  4510. 3:15:54outputs the current line it's on, which
  4511. 3:15:56can feel strange. However, it gets even
  4512. 3:15:59more bizarre. If I were to add a few
  4513. 3:16:01lines before the echo statement, the
  4514. 3:16:03value can change.
  4515. 3:16:06As you can see, the new value reflects
  4516. 3:16:09the new line number. To take things a
  4517. 3:16:11step further, we can output the constant
  4518. 3:16:13again to get a completely different
  4519. 3:16:15value. I'll add a break element to the
  4520. 3:16:18first echo statement for readability.
  4521. 3:16:22Once again, the correct line numbers get
  4522. 3:16:24outputed, which is strange. The idea of
  4523. 3:16:27a constant is that the value can never
  4524. 3:16:30change. That's still partially true.
  4525. 3:16:32Developers are not allowed to update
  4526. 3:16:34constants. If we were to attempt to
  4527. 3:16:37update a magic constant, PHP would throw
  4528. 3:16:39an error. Only PHP is allowed to
  4529. 3:16:42override the values of a magic constant.
  4530. 3:16:45Magic constants are a feature for
  4531. 3:16:47accessing information about the current
  4532. 3:16:49file. Conveniently, PHP manages these
  4533. 3:16:53variables on our behalf. What makes them
  4534. 3:16:55unique is that their values can change
  4535. 3:16:57based on where we use them. To give
  4536. 3:17:00another example, I'm going to replace
  4537. 3:17:02both constants with a constant called
  4538. 3:17:05file and directory. Be sure to include
  4539. 3:17:08two underscore characters before and
  4540. 3:17:10after their names.
  4541. 3:17:14The file constant contains the full
  4542. 3:17:16system path to the current file. Notice
  4543. 3:17:18how I said system path. It's not the
  4544. 3:17:21HTTP URL to the file, but the path to
  4545. 3:17:24the file on the current machine. Since
  4546. 3:17:26we're using Replet, the path points to
  4547. 3:17:28the file on Replet's servers. As for the
  4548. 3:17:31directory constant, it points to the
  4549. 3:17:34directory where the file can be found.
  4550. 3:17:36Both constants are completely useful.
  4551. 3:17:38More often than not, you're likely to
  4552. 3:17:40use them a few times in each of your PHP
  4553. 3:17:43applications. I highly recommend
  4554. 3:17:45familiarizing yourself with them. That's
  4555. 3:17:47about it for magic constants. In the
  4556. 3:17:50next lecture, let's continue filling in
  4557. 3:17:52the gaps of PHP.
  4558. 3:17:54[Music]
  4559. 3:18:05In this lecture, we're going to explore
  4560. 3:18:08an alternative syntax for creating
  4561. 3:18:10constants. PHP has two features for
  4562. 3:18:13creating constants. So far, we've been
  4563. 3:18:16using the const keyword. The other
  4564. 3:18:18solution is to use the define function.
  4565. 3:18:21Let's look at an example in our script.
  4566. 3:18:23Let's replace the current code with a
  4567. 3:18:25function called define.
  4568. 3:18:29The define function has two parameters.
  4569. 3:18:32The first parameter is the name of the
  4570. 3:18:34constants. Unlike before, we must pass
  4571. 3:18:36in the name as a string. Other than
  4572. 3:18:38that, the same rules and conventions
  4573. 3:18:41apply to constants. Let's set the name
  4574. 3:18:43of the constant to fu.
  4575. 3:18:47Next, the second parameter must contain
  4576. 3:18:50the value of the constant. Let's set the
  4577. 3:18:53value to hello world.
  4578. 3:18:57Lastly, we can access the value of the
  4579. 3:18:59constant by its name like any other
  4580. 3:19:01constant. After defining the constant,
  4581. 3:19:04let's echo the value
  4582. 3:19:07in the preview. The value from the
  4583. 3:19:09constant appears. That's great. But what
  4584. 3:19:12are the differences between the define
  4585. 3:19:14function and const keyword? Well, the
  4586. 3:19:17const keyword was introduced for
  4587. 3:19:19defining constants from within a class.
  4588. 3:19:21Classes are a feature of PHP we haven't
  4589. 3:19:24had the opportunity to discuss yet. They
  4590. 3:19:26are going to be a prominent topic in an
  4591. 3:19:29upcoming lecture. However, when we get
  4592. 3:19:31to classes, the define function cannot
  4593. 3:19:34be used from within a class, whereas the
  4594. 3:19:36const keyword can. Another difference
  4595. 3:19:39between both solutions is being able to
  4596. 3:19:41use the define function from within a
  4597. 3:19:44conditional statement. For example, I'm
  4598. 3:19:46going to wrap the define function in an
  4599. 3:19:49if statement.
  4600. 3:19:52The condition for this statement will be
  4601. 3:19:54true.
  4602. 3:19:57If we run the script, PHP doesn't have a
  4603. 3:20:00problem with our code. It's completely
  4604. 3:20:02acceptable to define constants from
  4605. 3:20:04within a conditional statement. However,
  4606. 3:20:07this behavior is not possible with the
  4607. 3:20:09const keyword. I'm going to quickly
  4608. 3:20:12convert the constant from the define
  4609. 3:20:14function to the const keyword.
  4610. 3:20:18This time an error gets produced by PHP.
  4611. 3:20:21If we use the const keyword, PHP is
  4612. 3:20:24unable to create the constant from
  4613. 3:20:26within a conditional statement. But the
  4614. 3:20:29question is, why would we want to define
  4615. 3:20:31a constant from within a conditional
  4616. 3:20:33statement? The most common use case is
  4617. 3:20:35to avoid redefining a constant. For
  4618. 3:20:38example, I'm going to revert the
  4619. 3:20:40constant definition to the define
  4620. 3:20:42function
  4621. 3:20:45afterward. Instead of passing in true,
  4622. 3:20:48let's use a function called defined.
  4623. 3:20:52The defined function is not to be
  4624. 3:20:55confused with the defined function. They
  4625. 3:20:57have similar names, but the function
  4626. 3:20:59we're using has the letter D at the end.
  4627. 3:21:02This function can be used to check that
  4628. 3:21:04a constant has been defined. If you're
  4629. 3:21:06developing a large application, you may
  4630. 3:21:08have dozens of files. It's not uncommon
  4631. 3:21:11to define the same constant from within
  4632. 3:21:13multiple files. This is especially true
  4633. 3:21:16if you have an application that can
  4634. 3:21:18support plugins. In these cases, you may
  4635. 3:21:21end up defining a constant twice. As we
  4636. 3:21:23know, PHP does not allow a constant to
  4637. 3:21:26be redefined after one has been created.
  4638. 3:21:29To avoid errors, it's common practice to
  4639. 3:21:32check that the constant has been
  4640. 3:21:34created, which is what the defined
  4641. 3:21:36function can do. This function accepts
  4642. 3:21:38the name of the constant. Let's pass in
  4643. 3:21:41fu.
  4644. 3:21:44A true boolean value is returned by this
  4645. 3:21:47function only if the constant has been
  4646. 3:21:49defined. However, we want to check that
  4647. 3:21:52the constant hasn't been defined because
  4648. 3:21:54if it hasn't, we can go ahead with
  4649. 3:21:56creating it. Luckily, we know an
  4650. 3:21:58operator that can check for a false
  4651. 3:22:00value. Do you remember what it is? I'll
  4652. 3:22:03give you a moment to think about this.
  4653. 3:22:07It's the not operator. We can add it to
  4654. 3:22:10the beginning of the condition like so.
  4655. 3:22:12Essentially, we're checking that the
  4656. 3:22:14constant hasn't been created. If we look
  4657. 3:22:17at the preview, everything is working as
  4658. 3:22:19before. So, that's the define function.
  4659. 3:22:22It's an alternative solution to using
  4660. 3:22:24the const keyword. You can use whatever
  4661. 3:22:26you prefer. In my personal preference, I
  4662. 3:22:29like using the const keyword because
  4663. 3:22:31it's easier to read. It's also easy to
  4664. 3:22:34distinguish from the define function,
  4665. 3:22:36which checks for a constant. However,
  4666. 3:22:38you're more than welcome to continue
  4667. 3:22:40using the define function. In the next
  4668. 3:22:43lecture, let's move away from constants
  4669. 3:22:45to talk about other features of PHP.
  4670. 3:22:49[Music]
  4671. 3:23:00In this lecture, we're going to learn
  4672. 3:23:02about a function called unset. Whenever
  4673. 3:23:05we define a variable, servers allocate
  4674. 3:23:08memory for that variable. Memory is not
  4675. 3:23:10released until the script has finished
  4676. 3:23:13executing. However, we may want to free
  4677. 3:23:15memory before that happens. PHP gives us
  4678. 3:23:18the power to delete a variable after
  4679. 3:23:20it's been created by using a function
  4680. 3:23:23called unset. Let's give it a try. In
  4681. 3:23:26our script, let's replace the current
  4682. 3:23:28code with a variable called name. Its
  4683. 3:23:30value will be John.
  4684. 3:23:34Next, let's try echoing the variable.
  4685. 3:23:39So far, nothing new. After echoing the
  4686. 3:23:41variable, we may not need it anymore.
  4687. 3:23:44That's going to be a common thing in
  4688. 3:23:45your code. Sometimes you won't need your
  4689. 3:23:48variables to stick around after they've
  4690. 3:23:50served their use. We can unset the
  4691. 3:23:52variable by calling the unset function
  4692. 3:23:55with the variable passed in.
  4693. 3:23:59Calling the unset function doesn't cause
  4694. 3:24:02anything to happen on the screen. Behind
  4695. 3:24:04the scenes, it's deleting the variable.
  4696. 3:24:06To prove this, we can try echoing the
  4697. 3:24:09name again.
  4698. 3:24:12This time, we got an error from PHP.
  4699. 3:24:14Despite defining it earlier, the
  4700. 3:24:16variable becomes undefined because we've
  4701. 3:24:19deleted it from memory. We're not
  4702. 3:24:21limited to deleting an entire variable.
  4703. 3:24:24What if we have an array? Individual
  4704. 3:24:26items from an array can be deleted. For
  4705. 3:24:28example, let's try updating the name
  4706. 3:24:31variable by converting it into an array.
  4707. 3:24:34Wrap the value with a pair of square
  4708. 3:24:36brackets.
  4709. 3:24:39Next, let's add an additional set of
  4710. 3:24:41random names.
  4711. 3:24:44Lastly, I'm going to update the variable
  4712. 3:24:47name from name to names. Not a necessary
  4713. 3:24:50step, but recommended. As I stated
  4714. 3:24:52before, it's considered good practice to
  4715. 3:24:55use the plural version of a word for
  4716. 3:24:57variables that store arrays. After
  4717. 3:25:00updating the variable, I'm going to
  4718. 3:25:02update the next few lines of code to
  4719. 3:25:04echo and unset the second item in the
  4720. 3:25:06array.
  4721. 3:25:09As a reminder, the second item of the
  4722. 3:25:12array has an index of one. PHP starts
  4723. 3:25:15counting at zero. Therefore, the second
  4724. 3:25:17item's index is one. If we run the code,
  4725. 3:25:20we get the same behavior as before. The
  4726. 3:25:23name appears in the preview, gets unset,
  4727. 3:25:25and then PHP throws an error because the
  4728. 3:25:28item doesn't exist anymore. But what
  4729. 3:25:31about the rest of the array? To find
  4730. 3:25:32out, we should try rendering the entire
  4731. 3:25:35array. One option is to use the variable
  4732. 3:25:38dump function. On the other hand, I
  4733. 3:25:40think this is a great opportunity to
  4734. 3:25:42introduce you to another function for
  4735. 3:25:44viewing the contents of the array.
  4736. 3:25:46Replace the echo statements with a
  4737. 3:25:48function called print r. While you're
  4738. 3:25:50doing that, make sure that you're
  4739. 3:25:52passing in the entire array instead of a
  4740. 3:25:54specific item.
  4741. 3:25:57The print r function was designed for
  4742. 3:26:00outputting an entire array. The main
  4743. 3:26:02difference between the print r function
  4744. 3:26:04and variable dump function is that the
  4745. 3:26:07print r function does not print the data
  4746. 3:26:09types. There's less information in the
  4747. 3:26:11output. Only the values appear in the
  4748. 3:26:14preview. you're not always going to be
  4749. 3:26:16interested in the data type of each item
  4750. 3:26:18in the array. As you can see, the array
  4751. 3:26:21continues to persist. Let's take a
  4752. 3:26:23closer look at the array. The first
  4753. 3:26:25array has three items, whereas the
  4754. 3:26:27second array has two items. In the
  4755. 3:26:29second array, the item with an index of
  4756. 3:26:32one is completely missing. PHP goes from
  4757. 3:26:35zero to two, skipping one. This confirms
  4758. 3:26:38that the item was deleted. Now, with
  4759. 3:26:41that being said, you may want to reindex
  4760. 3:26:43the array. It can be awkward to have
  4761. 3:26:45missing indexes. PHP does not
  4762. 3:26:48automatically reindex an array after an
  4763. 3:26:50item has been unset. We can manually
  4764. 3:26:53reindex the array by updating the names
  4765. 3:26:56variable to a function called array
  4766. 3:26:59values.
  4767. 3:27:02The array values function accepts the
  4768. 3:27:04array to reindex. Let's pass that in.
  4769. 3:27:09It'll return the reindexed array as a
  4770. 3:27:12result. Looking at the preview, the
  4771. 3:27:14second array is no longer missing the
  4772. 3:27:16one index. The last item in the array
  4773. 3:27:19got reindexed into the correct number.
  4774. 3:27:21Overall, unsetting variables is
  4775. 3:27:24incredibly easy. With one line of code,
  4776. 3:27:26you can release memory from your script.
  4777. 3:27:28In the next lecture, let's take a moment
  4778. 3:27:30to talk about PHP's documentation.
  4779. 3:27:34[Music]
  4780. 3:27:46In this lecture, we're going to take a
  4781. 3:27:48moment to talk about PHP's
  4782. 3:27:50documentation. For the past few
  4783. 3:27:52lectures, I've introduced a few
  4784. 3:27:54functions to you, but didn't show you
  4785. 3:27:56how I found them. As mentioned before,
  4786. 3:27:58PHP has hundreds of functions. It's
  4787. 3:28:01impossible to cover every single one.
  4788. 3:28:03But how do you know which functions are
  4789. 3:28:05worth remembering and which aren't? That
  4790. 3:28:07isn't an easy question to answer. There
  4791. 3:28:10isn't an official list of recommended
  4792. 3:28:11functions to remember. However, not all
  4793. 3:28:14hope is lost. Learning to read the
  4794. 3:28:16documentation is one of the most crucial
  4795. 3:28:18skills you can pick up as a developer.
  4796. 3:28:21That's my goal for this lecture. In the
  4797. 3:28:23resource section of this lecture, I
  4798. 3:28:25provide a link to PHP's manual. On this
  4799. 3:28:28page, PHP provides information on
  4800. 3:28:31various topics from syntax to security
  4801. 3:28:33practices. There's a wealth of knowledge
  4802. 3:28:36to be found in the manual. One of the
  4803. 3:28:38sections you may find yourself
  4804. 3:28:40interested in is the functions
  4805. 3:28:42reference. Search for it and click the
  4806. 3:28:44link.
  4807. 3:28:47The functions reference provides a
  4808. 3:28:49complete list of functions available in
  4809. 3:28:51PHP. You can perform various actions
  4810. 3:28:54from interacting with a database to
  4811. 3:28:56creating files on a server. I highly
  4812. 3:28:59recommend bookmarking this page as you
  4813. 3:29:01may need it in the future. Regardless of
  4814. 3:29:03what function you plan on using, the
  4815. 3:29:06structure of a function's documentation
  4816. 3:29:08page is the same. Let's look at an
  4817. 3:29:10example in the top right. Search for a
  4818. 3:29:13function called array values.
  4819. 3:29:17We learned about this function from the
  4820. 3:29:19previous lecture. Before using a
  4821. 3:29:21function, you should always take the
  4822. 3:29:23time to view the documentation. But how
  4823. 3:29:26do you read a function's documentation
  4824. 3:29:27page? Let's try going through each
  4825. 3:29:30section one by one. First and foremost,
  4826. 3:29:33a list of supported PHP versions is
  4827. 3:29:36provided under the function name. Not
  4828. 3:29:38all functions are available in every
  4829. 3:29:40version of PHP. Some functions have been
  4830. 3:29:42around since the early days of PHP.
  4831. 3:29:45Other functions were introduced in newer
  4832. 3:29:47versions. If a function is not behaving
  4833. 3:29:50as expected, you should check that it's
  4834. 3:29:52available in your current version of
  4835. 3:29:54PHP. Luckily, we're using the latest
  4836. 3:29:56version of PHP, which is version 8. The
  4837. 3:29:59array values function is available in
  4838. 3:30:02this version. Below the version list,
  4839. 3:30:04we're given a short description of the
  4840. 3:30:06function. In my opinion, the most
  4841. 3:30:09important portion of the description is
  4842. 3:30:11the function definition. This portion of
  4843. 3:30:14the description has two critical pieces
  4844. 3:30:16of information. Firstly, the functions
  4845. 3:30:19parameters are listed in the definition.
  4846. 3:30:21Pay close attention to the data type. If
  4847. 3:30:24you pass in the incorrect data type, PHP
  4848. 3:30:27may attempt type juggling. Whenever
  4849. 3:30:29possible, you should avoid that behavior
  4850. 3:30:31by passing in the correct data type.
  4851. 3:30:34Otherwise, you might encounter errors or
  4852. 3:30:36unexpected behavior. The second critical
  4853. 3:30:39piece of information is the return type,
  4854. 3:30:41which is always written after the colon
  4855. 3:30:43character in the function definition.
  4856. 3:30:45According to the documentation, we can
  4857. 3:30:48expect the function to return an array.
  4858. 3:30:50It's a great summary, but we may require
  4859. 3:30:53more information. Luckily, PHP has us
  4860. 3:30:55covered. Below the description, the
  4861. 3:30:58parameters and return values sections
  4862. 3:31:01provide additional context. Honestly,
  4863. 3:31:04there's not much to this function. It
  4864. 3:31:06just accepts an array and returns a
  4865. 3:31:08reindexed array. Regardless, you're most
  4866. 3:31:11likely to check out these sections of
  4867. 3:31:13the documentation page. Most functions
  4868. 3:31:16have these sections. PHP is very good at
  4869. 3:31:19being consistent. There are more
  4870. 3:31:21sections worth looking at. Afterward,
  4871. 3:31:23there's a section with an example. This
  4872. 3:31:26section gives you the opportunity to
  4873. 3:31:28look at how to use a function. Sometimes
  4874. 3:31:30you may find multiple examples. In this
  4875. 3:31:33case, we have a single example of
  4876. 3:31:36reindexing an array. Moving along, we
  4877. 3:31:39have a section called see. Also, you may
  4878. 3:31:42come across a function that doesn't
  4879. 3:31:44really solve the problem you're facing.
  4880. 3:31:46If that's the case, you might want to
  4881. 3:31:48check out this section of a functions
  4882. 3:31:50documentation page. This section
  4883. 3:31:52provides an alternative list of
  4884. 3:31:54functions that you can use. Oftent
  4885. 3:31:56times, they're similar to the function
  4886. 3:31:59you're looking at currently. For
  4887. 3:32:01example, PHP recommends the array keys
  4888. 3:32:04function for grabbing an array of keys
  4889. 3:32:06instead of the arrays values. If you
  4890. 3:32:08need the keys of an array, this function
  4891. 3:32:10is a great alternative. Let's keep
  4892. 3:32:13moving forward. One of the last sections
  4893. 3:32:15is user contributed notes. As mentioned
  4894. 3:32:18before, PHP is an open-source language.
  4895. 3:32:22Volunteers from all over the world
  4896. 3:32:24contribute to PHP's growth and
  4897. 3:32:26maintenance. Part of that effort is
  4898. 3:32:28improving the documentation. This
  4899. 3:32:30section has notes from other developers
  4900. 3:32:32of PHP. Developers can freely submit
  4901. 3:32:36additional pieces of information about a
  4902. 3:32:38function that is missing from the
  4903. 3:32:39current documentation. If you ever need
  4904. 3:32:42more examples or information, this
  4905. 3:32:44section is worth checking out. There's
  4906. 3:32:46actually one more section that I want to
  4907. 3:32:49show you that isn't available on this
  4908. 3:32:51functions documentation page. We'll need
  4909. 3:32:53to look at a different function. In the
  4910. 3:32:55search bar, search for the define
  4911. 3:32:57function.
  4912. 3:33:00Like usual, we're going to encounter the
  4913. 3:33:03same sections from the previous
  4914. 3:33:04function. However, there's one
  4915. 3:33:06additional section you may discover.
  4916. 3:33:08Scrolling through the page, there's a
  4917. 3:33:10section called change log. PHP is an
  4918. 3:33:14evolving language. Not all functions
  4919. 3:33:16remain the same across different
  4920. 3:33:17versions. The PHP team may take the time
  4921. 3:33:21to improve some functions. If a function
  4922. 3:33:23changes, those changes are documented in
  4923. 3:33:26this section. For example, since PHP 7,
  4924. 3:33:30the define function allows developers to
  4925. 3:33:33use arrays as values. Prior to this
  4926. 3:33:35version, arrays were not supported in
  4927. 3:33:38constants. This is important to
  4928. 3:33:40understand. Just because a function
  4929. 3:33:42works in one version doesn't mean it'll
  4930. 3:33:44work the same way in another version.
  4931. 3:33:47Occasionally, you may find yourself
  4932. 3:33:49using a function that behaves
  4933. 3:33:51differently in older projects. Checking
  4934. 3:33:53out this section of the page can help
  4935. 3:33:55you find out why. All right, so that's
  4936. 3:33:58about it for the documentation of PHP.
  4937. 3:34:01Throughout the course, I'll be sure to
  4938. 3:34:03mention other helpful information worth
  4939. 3:34:05looking at. For now, this information
  4940. 3:34:07should suffice. Let's get back into
  4941. 3:34:10programming in the following lecture.
  4942. 3:34:14[Music]
  4943. 3:34:24In this lecture, we will learn how to
  4944. 3:34:26round numbers with PHP. Not only that,
  4945. 3:34:29we're going to build upon our reading
  4946. 3:34:31comprehension of PHP's documentation.
  4947. 3:34:34Check out the resource section of this
  4948. 3:34:36lecture for links to three functions.
  4949. 3:34:39We're going to be exploring three
  4950. 3:34:41functions called seal, floor, and round.
  4951. 3:34:44Starting with the floor function, let's
  4952. 3:34:47check out the documentation page. Most
  4953. 3:34:49of the information is self-explanatory.
  4954. 3:34:52However, there's one piece of
  4955. 3:34:54information I want to highlight from the
  4956. 3:34:56page. and the description. The data type
  4957. 3:34:59for the number parameter is a union
  4958. 3:35:01type. You can identify union types by
  4959. 3:35:04the pipe character. As you can see from
  4960. 3:35:06this example, the seal function accepts
  4961. 3:35:08an integer or float. Just wanted to
  4962. 3:35:11point this out as you may come across
  4963. 3:35:14union types in the documentation. All
  4964. 3:35:16right, let's try using this function.
  4965. 3:35:19Switch over to your replet project in
  4966. 3:35:21the script. We're going to echo the
  4967. 3:35:23following number 5.454. 454.
  4968. 3:35:29So far, we've learned that casting a
  4969. 3:35:32float to an integer causes PHP to round
  4970. 3:35:35the number. However, it's not truly
  4971. 3:35:37rounding the number. It just strips out
  4972. 3:35:39the decimal values. For example, let's
  4973. 3:35:42try type casting the number into an
  4974. 3:35:45integer like so.
  4975. 3:35:48In the preview, we've given the number
  4976. 3:35:50without the decimal values. But once
  4977. 3:35:53again, PHP is not rounding the values.
  4978. 3:35:56If we want to round the value, we can
  4979. 3:35:58use one of three functions. One of the
  4980. 3:36:01functions is called floor.
  4981. 3:36:05This function always rounds a number
  4982. 3:36:07down. As you can see, we get the same
  4983. 3:36:09result. But what if the number was
  4984. 3:36:11something like 5.9?
  4985. 3:36:15Regardless, the number gets rounded
  4986. 3:36:17down. Now, you may be thinking, how is
  4987. 3:36:20this any different from type casting the
  4988. 3:36:22value into an integer? It seems like we
  4989. 3:36:24get the same results. However, that's
  4990. 3:36:27not entirely true. PHP is attempting to
  4991. 3:36:30round the number when we use the floor
  4992. 3:36:32function. To prove this, let me show you
  4993. 3:36:34a different example. Instead of echoing
  4994. 3:36:37the floor function, let's type cast a
  4995. 3:36:39negative number like minus0.1.
  4996. 3:36:45Type casting this value into an integer
  4997. 3:36:47produces zero. But what do you think
  4998. 3:36:50would happen if we were to use the floor
  4999. 3:36:52function instead? Let's find out.
  5000. 3:36:56Upon replacing the type cast with the
  5001. 3:36:59floor function, we get minus1. That
  5002. 3:37:02makes sense, right? When we type cast a
  5003. 3:37:04number, PHP strips the.1 decimal which
  5004. 3:37:08leaves us with zero. I always encourage
  5005. 3:37:11students to use these functions to round
  5006. 3:37:14values instead of type casting the value
  5007. 3:37:16as it'll always produce reliable
  5008. 3:37:18results. Let's keep exploring the other
  5009. 3:37:21rounding functions. Another function
  5010. 3:37:23that I linked to was the seal function.
  5011. 3:37:26This function behaves similarly to the
  5012. 3:37:28floor function. It'll round a number,
  5013. 3:37:31but the number will be rounded up. For
  5014. 3:37:33example, let's try rounding a number
  5015. 3:37:35like 5.454
  5016. 3:37:37with the seal function.
  5017. 3:37:41This time we got six. PHP gives us the
  5018. 3:37:44option of rounding down or up. But what
  5019. 3:37:46if we don't want to round to a whole
  5020. 3:37:48number? Sometimes we may want to round
  5021. 3:37:50with better precision. Well, it turns
  5022. 3:37:52out there is such a function called
  5023. 3:37:54round. Let's check out the documentation
  5024. 3:37:56for this function. Unlike the other two
  5025. 3:37:59functions, there are more parameters.
  5026. 3:38:01The round function was introduced for
  5027. 3:38:04customizing the rounding functionality
  5028. 3:38:06offered by PHP. To understand how this
  5029. 3:38:09function works, let's go through the
  5030. 3:38:11documentation. In the description, the
  5031. 3:38:14functions parameters have default
  5032. 3:38:16values. Looking closely, the assignment
  5033. 3:38:18operator is present in the definition.
  5034. 3:38:21Whenever you see the assignment
  5035. 3:38:23operator, the documentation is telling
  5036. 3:38:25you two things. Firstly, the parameter
  5037. 3:38:28is optional. Secondly, it tells you what
  5038. 3:38:30the default value of that parameter is.
  5039. 3:38:33The first parameter is the number to
  5040. 3:38:36round. The second parameter is called
  5041. 3:38:38precision with a default value of zero.
  5042. 3:38:41The last parameter is called mode with a
  5043. 3:38:43default value of constant. But how do I
  5044. 3:38:46know it's a constant? I don't know for
  5045. 3:38:48sure because we haven't read the
  5046. 3:38:50description from the parameter section
  5047. 3:38:52of the documentation. However, two signs
  5048. 3:38:56lead me to believe this is a constant.
  5049. 3:38:58Firstly, the value is written in all
  5050. 3:39:01uppercase letters. As we know it's
  5051. 3:39:03common for PHP developers to use all
  5052. 3:39:06uppercase letters for constant names.
  5053. 3:39:08This convention is followed by the PHP
  5054. 3:39:11language itself. Secondly, the name of
  5055. 3:39:13the constant starts with PHP which is
  5056. 3:39:16commonly used by the PHP language. Just
  5057. 3:39:19to make sure, let's read the
  5058. 3:39:21descriptions of these parameters.
  5059. 3:39:23According to the description of the
  5060. 3:39:25precision parameter, this parameter
  5061. 3:39:27allows us to configure the number of
  5062. 3:39:29digits that can be present in the
  5063. 3:39:31number. If we look back at the function
  5064. 3:39:34definition, the precision is set to
  5065. 3:39:36zero. Therefore, all decimal values are
  5066. 3:39:38removed from the result. We can override
  5067. 3:39:41this behavior by passing in a value for
  5068. 3:39:43this parameter. Moving along, the mode
  5069. 3:39:46parameter allows us to configure the
  5070. 3:39:48rounding behavior. Should the number be
  5071. 3:39:51rounded down or up when the number is at
  5072. 3:39:53the halfway point? By default, the
  5073. 3:39:55number goes up. We can change this
  5074. 3:39:57behavior by using one of the constants
  5075. 3:40:00listed on the documentation page. All
  5076. 3:40:02right, so we've gotten an overview of
  5077. 3:40:05this function. Let's try using it. Head
  5078. 3:40:07back to replet. Currently, we're
  5079. 3:40:09rounding the number with the seal
  5080. 3:40:11function. Let's replace it with the
  5081. 3:40:13round function.
  5082. 3:40:16We get the number five in the preview.
  5083. 3:40:19Despite having additional parameters,
  5084. 3:40:21they are completely optional. Therefore,
  5085. 3:40:23we can skip adding those into the
  5086. 3:40:25function. When you're working with
  5087. 3:40:27currencies, you may want to include
  5088. 3:40:29decimals. For example, most currencies
  5089. 3:40:32allow two digits in a decimal value. In
  5090. 3:40:35this case, we have three. According to
  5091. 3:40:38the documentation, the precision can be
  5092. 3:40:40configured by passing in an integer with
  5093. 3:40:42the exact number of decimals we'd like.
  5094. 3:40:45Let's pass in two.
  5095. 3:40:49Just like that, we've got a finetuned
  5096. 3:40:52rounded value. Let's take it a step
  5097. 3:40:54further by changing the number to 4.455.
  5098. 3:41:00Naturally, PHP rounds the number to
  5099. 3:41:034.46.
  5100. 3:41:05But what if we want to round the number
  5101. 3:41:06down when the last number is 5? That can
  5102. 3:41:09be configured with the third parameter.
  5103. 3:41:12We can pass in a constant called PHP
  5104. 3:41:15round half down.
  5105. 3:41:19This time the number changes to 4.45.
  5106. 3:41:23So, as you can see, the round function
  5107. 3:41:25is much more powerful than the floor or
  5108. 3:41:28seal functions. I recommend using it
  5109. 3:41:31whenever you need to round numbers with
  5110. 3:41:32better precision. I know I spent a lot
  5111. 3:41:35of time reading the documentation with
  5112. 3:41:37you. However, it's an invaluable skill
  5113. 3:41:39for a developer to understand how to
  5114. 3:41:42read the documentation. You're not going
  5115. 3:41:44to know every function in existence. In
  5116. 3:41:47these cases, deciphering a function's
  5117. 3:41:49documentation page will make you a
  5118. 3:41:51better developer. In the next lecture,
  5119. 3:41:53let's move on to conditional statements.
  5120. 3:41:57[Music]
  5121. 3:42:08In this lecture, we're going to look at
  5122. 3:42:11an alternative syntax for writing
  5123. 3:42:13conditional statements. PHP introduces a
  5124. 3:42:16different solution for writing
  5125. 3:42:18conditional statements for readability
  5126. 3:42:20in our script. Let's say we wanted to
  5127. 3:42:22welcome a user based on their
  5128. 3:42:24permission. Let's replace the current
  5129. 3:42:26code with a variable called permission.
  5130. 3:42:29Set the value to one.
  5131. 3:42:32I'll add a comment to describe this
  5132. 3:42:34variable. If the number is one, the user
  5133. 3:42:37is an administrator. If the number is
  5134. 3:42:39two, they're a moderator. Any other
  5135. 3:42:42number will be considered a guest.
  5136. 3:42:46Below the variable, add an if statement
  5137. 3:42:49with the following condition. Permission
  5138. 3:42:51equals equals equals 1.
  5139. 3:42:56Next, inside the block of code, let's
  5140. 3:42:59echo an H1 tag that says the following.
  5141. 3:43:02Hello admin.
  5142. 3:43:05Echoing HTML is completely valid.
  5143. 3:43:08Unfortunately, there's one huge flaw
  5144. 3:43:10with doing so. It's difficult to read
  5145. 3:43:12the string without syntax highlighting.
  5146. 3:43:15Every character in the string is a
  5147. 3:43:17single color. I really like syntax
  5148. 3:43:19highlighting for readability. Luckily,
  5149. 3:43:21we don't have to echo the element. An
  5150. 3:43:24alternative solution is to exit and
  5151. 3:43:26re-enter PHP mode. For example, I'll
  5152. 3:43:29replace the echo keyword with a closing
  5153. 3:43:32PHP tag.
  5154. 3:43:35Next, after the closing H1 tag, I'll add
  5155. 3:43:39an opening PHP tag.
  5156. 3:43:43Unlike before, we get syntax
  5157. 3:43:45highlighting. In my opinion, it's easier
  5158. 3:43:47to read. Let's continue adding more
  5159. 3:43:50conditions. Add an else if condition
  5160. 3:43:53with the following condition. Permission
  5161. 3:43:55equals equals 2.
  5162. 3:44:00Next, let's output another H1 tag with
  5163. 3:44:03the following message. Hello mod.
  5164. 3:44:08Lastly, let's add an else block.
  5165. 3:44:12This block outputs another H1 tag with
  5166. 3:44:15the following message. Hello guest.
  5167. 3:44:20So, we've got a working series of
  5168. 3:44:22conditional statements. There's just one
  5169. 3:44:24problem I have with it. Reading this
  5170. 3:44:26code is slightly difficult. It's not
  5171. 3:44:28uncommon to conditionally render HTML.
  5172. 3:44:31For this reason, PHP has a different
  5173. 3:44:33syntax for writing conditional
  5174. 3:44:35statements. First, let's put the
  5175. 3:44:38variable definition on a single line
  5176. 3:44:40with opening and closing PHP tags.
  5177. 3:44:45Next, let's replace our conditional
  5178. 3:44:47statements. I'll add the if statement
  5179. 3:44:50with the same condition. However, this
  5180. 3:44:52time, instead of using curly brackets,
  5181. 3:44:55I'll add a colon character after the
  5182. 3:44:57parenthesis.
  5183. 3:45:00Be sure to wrap the condition with
  5184. 3:45:02opening and closing PHP tags. After
  5185. 3:45:05these tags, add another pair of tags
  5186. 3:45:08with a keyword called endiff.
  5187. 3:45:12The alternative syntax doesn't require
  5188. 3:45:15opening and closing curly brackets.
  5189. 3:45:17Instead, we can denote the beginning of
  5190. 3:45:19the block of code by using a colon
  5191. 3:45:22character. The end of the block can be
  5192. 3:45:24denoted with the end if keyword. In
  5193. 3:45:26between these PHP tags, we can write our
  5194. 3:45:29HTML. Let's greet the admin again.
  5195. 3:45:34What about the else if and else blocks?
  5196. 3:45:37It's the same as before. After the H1
  5197. 3:45:40tag, add a pair of PHP tags with the
  5198. 3:45:43elseif keyword.
  5199. 3:45:46Two things worth noting. Firstly, the
  5200. 3:45:49else condition is written before the end
  5201. 3:45:51if keyword. This keyword acts as the
  5202. 3:45:54ending for the entire chain of
  5203. 3:45:56conditional statements, not just the if
  5204. 3:45:58statement. If we introduce the else if
  5205. 3:46:01keyword after the if keyword, PHP
  5206. 3:46:03assumes this is the end of the first if
  5207. 3:46:05keyword. Secondly, the else if keyword
  5208. 3:46:08cannot be written with a space between
  5209. 3:46:10the words. Previously, we have the
  5210. 3:46:12option of adding a space between the
  5211. 3:46:14else and if keywords, but this syntax
  5212. 3:46:17does not support that option. They must
  5213. 3:46:19be written as one word. All right, let's
  5214. 3:46:22write the condition.
  5215. 3:46:25Next, let's output an H1 tag to greet
  5216. 3:46:29the moderator.
  5217. 3:46:32Lastly, let's add the else block in
  5218. 3:46:34another pair of PHP tags.
  5219. 3:46:39Greet the guest.
  5220. 3:46:42We have the same result as before. PHP
  5221. 3:46:45introduces the syntax for readability.
  5222. 3:46:48The conditional statements work the
  5223. 3:46:50exact same. If you don't like this
  5224. 3:46:52syntax, that's perfectly fine. The other
  5225. 3:46:54solution works just as well. Generally,
  5226. 3:46:57it's recommended to use curly brackets
  5227. 3:46:59when you're only writing PHP code. If
  5228. 3:47:02you need to write HTML, this syntax is
  5229. 3:47:05considered to be superior for its
  5230. 3:47:07readability. That's how most developers
  5231. 3:47:09decide when to use which syntax. In the
  5232. 3:47:12next lecture, let's talk about another
  5233. 3:47:14topic related to conditional statements.
  5234. 3:47:18[Music]
  5235. 3:47:29In this lecture, we're going to talk
  5236. 3:47:31about optimizing our code. It's never
  5237. 3:47:34too early to talk about optimization.
  5238. 3:47:36So, what exactly are we going to
  5239. 3:47:38optimize? Not all functions are created
  5240. 3:47:40equal. Some functions can be executed
  5241. 3:47:43within an instant. Other functions may
  5242. 3:47:45take a while to finish. You're never
  5243. 3:47:47guaranteed how long your code will take
  5244. 3:47:49to run. In these cases, we should take
  5245. 3:47:52every opportunity to optimize our code.
  5246. 3:47:55For example, let's say the user's
  5247. 3:47:57permission was outsourced in a function.
  5248. 3:48:00Replace the permission variable with a
  5249. 3:48:02function called get permission.
  5250. 3:48:06From within this function, return one.
  5251. 3:48:10Lastly, let's update our conditions to
  5252. 3:48:13use this function instead of the
  5253. 3:48:15permission variable.
  5254. 3:48:18So, what we're doing is returning the
  5255. 3:48:20permission from the function. The same
  5256. 3:48:22result occurs in our page. But let's say
  5257. 3:48:25that this function takes a while to
  5258. 3:48:27execute. Believe it or not, our code is
  5259. 3:48:29inefficient. To find out why, let's try
  5260. 3:48:32extending the duration of our function.
  5261. 3:48:35Before returning a number, let's run a
  5262. 3:48:37function called sleep.
  5263. 3:48:41The sleep function instructs PHP to
  5264. 3:48:43pause its execution. We can set the
  5265. 3:48:46duration by passing in a number. Let's
  5266. 3:48:48wait 2 seconds.
  5267. 3:48:52After making that change, we can refresh
  5268. 3:48:54the page. This time, the result takes a
  5269. 3:48:57while to appear. Until 2 seconds have
  5270. 3:48:59passed, the welcome message won't
  5271. 3:49:01appear. So far, it isn't that bad.
  5272. 3:49:04However, what if our function returned
  5273. 3:49:06two instead of one?
  5274. 3:49:10Things become even slower. Upon
  5275. 3:49:12refreshing the page, we're going to be
  5276. 3:49:14waiting 4 seconds. But why? Let's look
  5277. 3:49:17at our code to understand the problem.
  5278. 3:49:19In the if condition, PHP executes the
  5279. 3:49:22get permission function. At this point,
  5280. 3:49:24we're waiting 2 seconds. Once the
  5281. 3:49:26function returns a value, PHP compares
  5282. 3:49:29the numbers which don't match. PHP moves
  5283. 3:49:32on to the next condition. Once again,
  5284. 3:49:35we're calling the get permission
  5285. 3:49:36function. Just like before, we must wait
  5286. 3:49:392 seconds before the values can be
  5287. 3:49:41compared. This is why our page is taking
  5288. 3:49:44a while to load. Imagine if we had
  5289. 3:49:46multiple conditions with the get
  5290. 3:49:48permission function. Users would be
  5291. 3:49:50waiting forever. It's perfectly fine to
  5292. 3:49:52call functions from within your
  5293. 3:49:54conditions. However, if you decide to do
  5294. 3:49:57so, you should realize that PHP must
  5295. 3:49:59wait for your function to finish
  5296. 3:50:01executing before completing the
  5297. 3:50:03comparison. Otherwise, your machine may
  5298. 3:50:06occupy too many resources. There are two
  5299. 3:50:09ways of resolving this issue. The first
  5300. 3:50:12solution is to outsource the value
  5301. 3:50:14returned by your function in a variable.
  5302. 3:50:16Above the conditional statements, add a
  5303. 3:50:19variable called permission with the get
  5304. 3:50:21permission function as the value.
  5305. 3:50:26Next, let's use the variable instead of
  5306. 3:50:28the function.
  5307. 3:50:31This is so much better. Now we're only
  5308. 3:50:33executing the function once instead of
  5309. 3:50:36for each condition. So what about the
  5310. 3:50:39second solution? Instead of using if
  5311. 3:50:41statements, we can use switch
  5312. 3:50:43statements. I'm going to quickly convert
  5313. 3:50:45this solution into a switch statement.
  5314. 3:50:47You don't have to follow along. Just
  5315. 3:50:49watch.
  5316. 3:50:58In this solution, we're calling the get
  5317. 3:51:00permission function from within the
  5318. 3:51:02switch statements. I'm adding a case for
  5319. 3:51:04each type of user followed by a message
  5320. 3:51:07to greet the user. It's the same thing
  5321. 3:51:09as before. Interestingly, the page does
  5322. 3:51:12not take 4 seconds to load. Switch
  5323. 3:51:15statements execute the expression passed
  5324. 3:51:17into the switch statement once.
  5325. 3:51:19Therefore, the get permission function
  5326. 3:51:22runs once. If the first case fails, PHP
  5327. 3:51:26does not execute the function again. It
  5328. 3:51:28just uses the value from before to
  5329. 3:51:30compare with the next case. We never
  5330. 3:51:33have to worry about a function being
  5331. 3:51:34called repeatedly, thus saving us the
  5332. 3:51:37trouble of storing the value in a
  5333. 3:51:39separate variable. Either solution is
  5334. 3:51:41valid. Use whatever you prefer. This
  5335. 3:51:44problem can also plague loops. For
  5336. 3:51:46example, let's say we wanted to grab an
  5337. 3:51:48array of users from a database.
  5338. 3:51:50Communicating with a database can take
  5339. 3:51:52time. Let's create a function called get
  5340. 3:51:55users.
  5341. 3:51:58From within this function, call the
  5342. 3:52:00sleep function for 2 seconds.
  5343. 3:52:04Next, return an array with random names.
  5344. 3:52:09Now that we have our function, let's try
  5345. 3:52:12creating a for loop. Add this loop below
  5346. 3:52:15the function.
  5347. 3:52:17Let's quickly add the following. I
  5348. 3:52:20equals Z I less than count get users I
  5349. 3:52:24++.
  5350. 3:52:27Lastly, let's echo the I variable.
  5351. 3:52:32Overall, most of this loop is familiar
  5352. 3:52:34to us. I'm introducing a new function
  5353. 3:52:36called count. We don't want the loop to
  5354. 3:52:38run forever. We only want the loop to
  5355. 3:52:40run for as many items as there are in
  5356. 3:52:42the loop. The count function can be used
  5357. 3:52:45for counting the items in an array.
  5358. 3:52:47it'll return the size as an integer.
  5359. 3:52:49That's why we're calling it in this
  5360. 3:52:51example. Similar to before, we're going
  5361. 3:52:53to be waiting 4 seconds for the page to
  5362. 3:52:56render. This is because PHP waits 2
  5363. 3:52:59seconds for the condition to finish.
  5364. 3:53:01It'll execute the block of code and then
  5365. 3:53:03check the condition again. This process
  5366. 3:53:06involves calling the get users function,
  5367. 3:53:08which takes 2 seconds to finish.
  5368. 3:53:12To avoid this scenario, we can outsource
  5369. 3:53:15the value from counting the array into a
  5370. 3:53:17separate variable like so.
  5371. 3:53:23I'm using a variable called user count
  5372. 3:53:26to get this information. This time the
  5373. 3:53:28page takes less time to load. Unlike
  5374. 3:53:31before, the function gets called once
  5375. 3:53:34unlike calling it every time the loop
  5376. 3:53:36checks the condition. Once again,
  5377. 3:53:38storing the results of a function in a
  5378. 3:53:40variable is the best way to go
  5379. 3:53:42sometimes, especially if you're using
  5380. 3:53:44the results in conditions or loops. Just
  5381. 3:53:47something to consider when working on
  5382. 3:53:49your application. In the future, when we
  5383. 3:53:51start working on the master project,
  5384. 3:53:54we'll get plenty of practice optimizing
  5385. 3:53:56our code with this technique. In the
  5386. 3:53:58next lecture, let's continue exploring
  5387. 3:54:00other topics of PHP.
  5388. 3:54:03[Music]
  5389. 3:54:15In this lecture, we're going to start
  5390. 3:54:17adding additional files to our project.
  5391. 3:54:19Realistically, most projects require
  5392. 3:54:22developers to write hundreds of lines of
  5393. 3:54:24code. With so much code, it can be
  5394. 3:54:27difficult to maintain a project when
  5395. 3:54:29everything is written within a single
  5396. 3:54:30file. For this reason, PHP allows us to
  5397. 3:54:34split our codebase into multiple files.
  5398. 3:54:36Let's look at an example. In our
  5399. 3:54:38project, we have two files called
  5400. 3:54:41index.php
  5401. 3:54:43and about.php.
  5402. 3:54:45At the moment, users have to type the
  5403. 3:54:47file name from within the address bar to
  5404. 3:54:49view each page. I don't think that
  5405. 3:54:51provides for a good user experience. The
  5406. 3:54:54most common solution to this problem is
  5407. 3:54:56to create a navigation menu. So, let's
  5408. 3:54:59do that. In the index.php file, clear
  5409. 3:55:03the contents of the file. In the files
  5410. 3:55:05place, add a navigation element.
  5411. 3:55:10Inside the navigation element, add two
  5412. 3:55:13anchor elements linking to the homepage
  5413. 3:55:16and about page.
  5414. 3:55:21Below the navigation, let's add some
  5415. 3:55:24text to identify the homepage.
  5416. 3:55:28In the preview, the navigation appears
  5417. 3:55:31as expected. Everything we've done so
  5418. 3:55:33far is with pure HTML. However, there's
  5419. 3:55:36just one problem. If I were to click on
  5420. 3:55:39the about link, I'm taken to the about
  5421. 3:55:41page. The navigation menu is completely
  5422. 3:55:44missing. This issue can be resolved by
  5423. 3:55:47copying and pasting the navigation over
  5424. 3:55:49to the about file. Seems simple, right?
  5425. 3:55:52Not so fast. What if we need to add,
  5426. 3:55:55remove, or change the links in the
  5427. 3:55:57navigation menu? What if we had more
  5428. 3:55:59than two pages? Sites can have hundreds
  5429. 3:56:02of pages. Copying and pasting the menu
  5430. 3:56:05is not scalable. If we copy the menu to
  5431. 3:56:08each page, we would have to modify every
  5432. 3:56:10file that has the navigation menu.
  5433. 3:56:13Ideally, the navigation menu should be
  5434. 3:56:15easy to modify. Rather than creating
  5435. 3:56:18copies of the navigation menu, let's
  5436. 3:56:20have a single copy in a separate file
  5437. 3:56:22inside our project. Create a file called
  5438. 3:56:25nav.php.
  5439. 3:56:29Next, I'm going to move the navigation
  5440. 3:56:32element from the index file to the
  5441. 3:56:34navigation file.
  5442. 3:56:38The question becomes, how can we load
  5443. 3:56:40the navigation file from both the index
  5444. 3:56:42and about files? PHP has a keyword
  5445. 3:56:45called include which instructs the
  5446. 3:56:48language to load a different file. The
  5447. 3:56:50include keyword accepts a path to a
  5448. 3:56:52file. Let's give it a try. Back in the
  5449. 3:56:55index file, try adding a pair of PHP
  5450. 3:56:58tags. Inside these tags, add the include
  5451. 3:57:01keyword.
  5452. 3:57:04The path to the file must be written as
  5453. 3:57:06a string. Let's set the path to nav.php.
  5454. 3:57:12Just like that, we've loaded the
  5455. 3:57:14navigation menu with a single line of
  5456. 3:57:16code. PHP handles loading the file for
  5457. 3:57:19us. As you can imagine, this feature is
  5458. 3:57:22incredibly helpful for splitting a page
  5459. 3:57:24into separate sections. You can create
  5460. 3:57:27separate files for the header, footer,
  5461. 3:57:29and sidebar. Let's try loading the
  5462. 3:57:32navigation for the about file. In this
  5463. 3:57:34file, I'm going to add the include
  5464. 3:57:36statement. Again, you can copy and paste
  5465. 3:57:39the line of code we had in the previous
  5466. 3:57:41file.
  5467. 3:57:43Great. Now, we can try clicking on the
  5468. 3:57:46links to test our navigation. If we
  5469. 3:57:48switch between pages, we're able to view
  5470. 3:57:50the respective pages content. Best of
  5471. 3:57:53all, we can update the navigation menu
  5472. 3:57:56once and all pages will be reflected
  5473. 3:57:58with those changes. We've just scratched
  5474. 3:58:01the surface. There's more we can do.
  5475. 3:58:03Back in the index file, PHP allows us to
  5476. 3:58:06include files multiple times. For
  5477. 3:58:09example, add the include keyword again
  5478. 3:58:11for the navigation file.
  5479. 3:58:15This time we get two navigation menus.
  5480. 3:58:18That's great and all, but this may be a
  5481. 3:58:20mistake. What if our navigation file had
  5482. 3:58:22PHP code for creating a constant? Let me
  5483. 3:58:25show you an example. In the navigation
  5484. 3:58:28file, add a pair of PHP tags above the
  5485. 3:58:31menu. Inside these tags, create a
  5486. 3:58:33constant called fu with a value of one.
  5487. 3:58:39You can use either the define function
  5488. 3:58:41or const keyword. It doesn't really
  5489. 3:58:44matter. After adding the constant, PHP
  5490. 3:58:47throws an error. The error states that
  5491. 3:58:49the constant has already been created.
  5492. 3:58:51The reason we get this error is because
  5493. 3:58:54we're including the navigation file
  5494. 3:58:56twice. Every time we include a file, PHP
  5495. 3:58:59executes the code inside that file. As a
  5496. 3:59:02result, we're attempting to define the
  5497. 3:59:04same constant twice. We can resolve this
  5498. 3:59:07issue by not including the file twice.
  5499. 3:59:09However, mistakes can happen. If we have
  5500. 3:59:12a large project, it's possible that we
  5501. 3:59:14may accidentally include a file twice.
  5502. 3:59:17To get around this dilemma, we can use
  5503. 3:59:19an alternative keyword. Back in the
  5504. 3:59:21index file, update the include keyword
  5505. 3:59:24to include once.
  5506. 3:59:28After swapping the keywords, the error
  5507. 3:59:30goes away. The include once keyword is
  5508. 3:59:32similar to the include keyword. It'll
  5509. 3:59:34embed the code from another file into
  5510. 3:59:37the current file. The main difference is
  5511. 3:59:39that the code is only executed once. If
  5512. 3:59:42we accidentally include the same file
  5513. 3:59:44twice, PHP will make sure that the code
  5514. 3:59:47only runs once, thus avoiding the error
  5515. 3:59:49encountered earlier. I'm going to remove
  5516. 3:59:52the second line of code. It's not
  5517. 3:59:54necessary to keep it around. In addition
  5518. 3:59:56to the include keyword, there's another
  5519. 3:59:59keyword available for including files
  5520. 4:00:01called require. Let's replace the
  5521. 4:00:03include once keyword with the require
  5522. 4:00:06keyword.
  5523. 4:00:08Similar to before, we get the same
  5524. 4:00:11result. Just like the include keyword,
  5525. 4:00:13the require keyword has an alternative
  5526. 4:00:15variation called require once.
  5527. 4:00:20So you may be wondering what are the
  5528. 4:00:22differences between the include and
  5529. 4:00:24require keywords. Both keywords can be
  5530. 4:00:27used for adding files to the current
  5531. 4:00:29file. The main difference is how they
  5532. 4:00:31handle errors. For example, let's try
  5533. 4:00:34changing the file from nav.php to
  5534. 4:00:37example.php.
  5535. 4:00:41This time an error gets thrown. There's
  5536. 4:00:43a lot of information, but the portion we
  5537. 4:00:46should focus on is the type of error.
  5538. 4:00:48PHP has thrown a fatal error. Whenever
  5539. 4:00:51PHP throws fatal errors, the rest of the
  5540. 4:00:54script does not execute. It doesn't
  5541. 4:00:56matter if we have more code to execute.
  5542. 4:00:59PHP stops running altogether. This is
  5543. 4:01:02proven by the fact that the message
  5544. 4:01:04after the require keyword is missing.
  5545. 4:01:07Let's replace this keyword with the
  5546. 4:01:09include keyword.
  5547. 4:01:12Once again, we get an error. This time
  5548. 4:01:15the homepage message appears after the
  5549. 4:01:17error. Looking closely at the error, PHP
  5550. 4:01:20marks the error as a warning, not a
  5551. 4:01:22fatal error. Unlike fatal errors,
  5552. 4:01:25warnings do not stop a script from
  5553. 4:01:27execution. PHP continues to run the next
  5554. 4:01:30line of code after the warning has been
  5555. 4:01:32thrown.
  5556. 4:01:35Understanding the differences is
  5557. 4:01:37crucial. Not every function or keyword
  5558. 4:01:40produces the same type of error.
  5559. 4:01:42Warnings do not stop a script from
  5560. 4:01:44execution. PHP simply outputs a message
  5561. 4:01:47and then proceeds to the next line of
  5562. 4:01:49code. Whereas fatal errors completely
  5563. 4:01:52stop the script from running after the
  5564. 4:01:55errors have been rendered on the page.
  5565. 4:01:59So which keyword should you use? Well,
  5566. 4:02:02that depends on if the file is
  5567. 4:02:03necessary. If the file is necessary, use
  5568. 4:02:06the require keyword. However, if your
  5569. 4:02:09application can function fine without
  5570. 4:02:11the file, use the include keyword.
  5571. 4:02:14Throughout the course, you'll see plenty
  5572. 4:02:16of examples. There's one more thing I
  5573. 4:02:19want to show you before moving on.
  5574. 4:02:21Believe it or not, files that are
  5575. 4:02:23included can return data. For example,
  5576. 4:02:26let's create a new file in our project
  5577. 4:02:28called example.php.
  5578. 4:02:33Inside this file, add an opening php
  5579. 4:02:36tag. We're going to use the return
  5580. 4:02:38keyword to return the number two.
  5581. 4:02:43The return keyword is not exclusive to
  5582. 4:02:46functions. We can add it outside of a
  5583. 4:02:48function to return data from the overall
  5584. 4:02:50file. Back on the index.php file, we can
  5585. 4:02:54store the data returned by our file in a
  5586. 4:02:57variable like we would with any other
  5587. 4:02:59type of value. Let's store the result in
  5588. 4:03:02a variable called data.
  5589. 4:03:06Lastly, let's echo the variable
  5590. 4:03:10in the preview. The number appears.
  5591. 4:03:13That's great. This feature can be handy
  5592. 4:03:15whenever you outsource the data in a
  5593. 4:03:17separate file instead of just
  5594. 4:03:19outsourcing HTML. Keep in mind the same
  5595. 4:03:22behavior applies to files as it does
  5596. 4:03:25functions. If we return data, the rest
  5597. 4:03:28of the file does not execute. For
  5598. 4:03:30example, let's head back to the example
  5599. 4:03:33file. Before returning a number, let's
  5600. 4:03:36return one.
  5601. 4:03:39After the return statement, echo the
  5602. 4:03:41number three.
  5603. 4:03:44In the preview, the numbers one are
  5604. 4:03:46rendered on the screen. However, the
  5605. 4:03:48number three is completely missing.
  5606. 4:03:51That's expected since the value is
  5607. 4:03:53echoed after the return statements. PHP
  5608. 4:03:56does not bother executing the rest of
  5609. 4:03:58the file after a value is returned.
  5610. 4:04:01Remember this behavior only applies to
  5611. 4:04:03the file that was included. The file
  5612. 4:04:06that performs the inclusion can continue
  5613. 4:04:09executing more code. That's about it for
  5614. 4:04:11including files with PHP. In the next
  5615. 4:04:14lecture, let's start diving into
  5616. 4:04:16features about functions.
  5617. 4:04:18[Music]
  5618. 4:04:30In this lecture, we're going to make our
  5619. 4:04:33functions more flexible by using the
  5620. 4:04:35spread operator in our functions. To
  5621. 4:04:38understand why this operator can be
  5622. 4:04:40useful, let's look at a scenario where
  5623. 4:04:42it can be useful in our script. Let's
  5624. 4:04:44replace the current code with a function
  5625. 4:04:47called sum.
  5626. 4:04:50The goal of this function will be to
  5627. 4:04:52calculate the sum of two numbers. Let's
  5628. 4:04:55add two parameters called a and b. Their
  5629. 4:04:58data types will be a union type of the
  5630. 4:05:00integer and float types.
  5631. 4:05:04Next, inside the function return a + b.
  5632. 4:05:10Lastly, let's use our new function by
  5633. 4:05:13echoing the function with the numbers
  5634. 4:05:15five and two.
  5635. 4:05:18Our function is working as expected.
  5636. 4:05:20It's possible to improve upon the
  5637. 4:05:22existing functionality. Sometimes we may
  5638. 4:05:25want to calculate the sum of three
  5639. 4:05:26numbers or seven numbers. At the moment,
  5640. 4:05:29our function is limited to two numbers.
  5641. 4:05:31One solution would be to add more
  5642. 4:05:33parameters. That would work, but it
  5643. 4:05:35would be tedious. It would be ridiculous
  5644. 4:05:37to have 20 parameters. An alternative
  5645. 4:05:40solution would be to create a variatic
  5646. 4:05:43function. A variatic function is a
  5647. 4:05:45function that accepts an unlimited
  5648. 4:05:47number of parameters. It's easy to
  5649. 4:05:49create. In the parameter list, replace
  5650. 4:05:52the parameters with a parameter called
  5651. 4:05:54numbers.
  5652. 4:05:57Next, before the parameters name, we can
  5653. 4:06:00add three dots, also known as the spread
  5654. 4:06:03operator. Actually, there isn't an
  5655. 4:06:05official name for this operator. Some
  5656. 4:06:07developers refer to it as the splat
  5657. 4:06:09operator. Either way, adding this
  5658. 4:06:12operator to a parameter instructs PHP to
  5659. 4:06:15accept unlimited arguments. Arguments
  5660. 4:06:18passed into the function are stored as
  5661. 4:06:20an array which we can access via the
  5662. 4:06:22parameter. Time hinting is still
  5663. 4:06:24supported before the arguments add the
  5664. 4:06:26integer and float data types.
  5665. 4:06:31Just to verify that the values are an
  5666. 4:06:33array of integers, let's call the
  5667. 4:06:35variable dump function with the numbers
  5668. 4:06:37parameters.
  5669. 4:06:40We do get a warning, but let's ignore
  5670. 4:06:42that for the time being. At the top of
  5671. 4:06:44the page, the variable dump function
  5672. 4:06:47outputs an array with the numbers passed
  5673. 4:06:49in. By using the spread operator, we've
  5674. 4:06:52created a variatic function. It can
  5675. 4:06:54accept multiple numbers. The next step
  5676. 4:06:57is to loop through the array to add the
  5677. 4:06:59numbers together. Fortunately, PHP has a
  5678. 4:07:02function for performing this exact
  5679. 4:07:04operator. Let's return the following.
  5680. 4:07:07Array sum numbers.
  5681. 4:07:11The array sum function loops through an
  5682. 4:07:14array of numbers and calculates the sum.
  5683. 4:07:16We don't have to create a custom loop to
  5684. 4:07:19achieve the same behavior. After
  5685. 4:07:21returning the value, the preview
  5686. 4:07:23displays the same result as before. This
  5687. 4:07:25time we can pass in additional numbers
  5688. 4:07:28to the function.
  5689. 4:07:31Our function will be able to handle any
  5690. 4:07:33number of arguments. There is one thing
  5691. 4:07:36to keep in mind when using variatic
  5692. 4:07:38functions. The variatic argument must
  5693. 4:07:41always be the last parameter. For
  5694. 4:07:43example, let's say we wanted to add the
  5695. 4:07:45option to dump the array before
  5696. 4:07:47returning the value. Add a boolean
  5697. 4:07:50parameter called dump array.
  5698. 4:07:54At the top of the function, add a
  5699. 4:07:56conditional statement with the dump
  5700. 4:07:58array parameter as the condition.
  5701. 4:08:02Next, inside the conditional block of
  5702. 4:08:05code, call the variable dump function
  5703. 4:08:07with the numbers parameter.
  5704. 4:08:11Lastly, let's pass in false as the first
  5705. 4:08:14argument to the sum function.
  5706. 4:08:18So far, everything is working as
  5707. 4:08:20intended. We can pass in true or false
  5708. 4:08:23to affect the behavior of the function.
  5709. 4:08:26However, what if we switch the
  5710. 4:08:28parameters? Let's find out.
  5711. 4:08:32After doing so, PHP throws a fatal
  5712. 4:08:35error. The message is cut and clear.
  5713. 4:08:37It's informing us that our variatic
  5714. 4:08:39parameter must be last, which makes
  5715. 4:08:42sense, right? After all, the dump array
  5716. 4:08:44parameter can never be assigned a value
  5717. 4:08:47if the numbers array takes in all the
  5718. 4:08:49values. So, keep this in mind when using
  5719. 4:08:52variatic functions. In the next lecture,
  5720. 4:08:55let's keep exploring functions.
  5721. 4:08:57[Music]
  5722. 4:09:08In this lecture, we're going to add
  5723. 4:09:10named arguments to our function. Named
  5724. 4:09:13arguments are a new feature of PHP8. If
  5725. 4:09:16you're using an older version of PHP,
  5726. 4:09:19this feature won't work. Named arguments
  5727. 4:09:21are a feature for passing a value to a
  5728. 4:09:24specific parameter. Let's look at an
  5729. 4:09:26example by simplifying our function.
  5730. 4:09:28First, we should update our function to
  5731. 4:09:31accept three parameters. Instead of
  5732. 4:09:33creating a variatic function, let's
  5733. 4:09:35revert the function to its original
  5734. 4:09:37solution, which was to accept two
  5735. 4:09:40numbers called A and B.
  5736. 4:09:44The dump array parameter can be removed
  5737. 4:09:47along with the conditional statement.
  5738. 4:09:51Afterward, update the return statement
  5739. 4:09:54to add the A and B parameters.
  5740. 4:09:58Lastly, let's update our echo statement
  5741. 4:10:01by passing in five and two.
  5742. 4:10:05Awesome. We've got something similar to
  5743. 4:10:07what we had before. So, how do named
  5744. 4:10:10arguments work? Currently, PHP assigns
  5745. 4:10:13the values to our parameters based on
  5746. 4:10:15the order of the values passed in. The
  5747. 4:10:18number five is assigned to the A
  5748. 4:10:20parameter and the number two is assigned
  5749. 4:10:23to the B parameter. What if we want to
  5750. 4:10:25reverse those assignments? Well, that's
  5751. 4:10:28possible with named arguments. Before
  5752. 4:10:30the values, we can add the name of the
  5753. 4:10:33parameters like so. B colon and A colon.
  5754. 4:10:40By writing the name of the parameters
  5755. 4:10:42before the values, PHP handles assigning
  5756. 4:10:45each value to the correct parameter
  5757. 4:10:47regardless of the order. We can prove
  5758. 4:10:50this further by dumping both parameters
  5759. 4:10:52from within the function. As you can
  5760. 4:10:54see, the B parameter holds the number
  5761. 4:10:57two and the A parameter holds the number
  5762. 4:11:00five. In our example, using named
  5763. 4:11:02arguments is redundant. The result is
  5764. 4:11:05going to be the same. So why would you
  5765. 4:11:07want to use named arguments? In the
  5766. 4:11:10resource section of this lecture, I
  5767. 4:11:12provide a link to a function called set
  5768. 4:11:15cookies. PHP has a few functions with
  5769. 4:11:18more than five parameters. Sometimes you
  5770. 4:11:21may have custom functions with a lot of
  5771. 4:11:23parameters. Looking closely at the
  5772. 4:11:26definition, some parameters are
  5773. 4:11:28optional. Let's imagine that we wanted
  5774. 4:11:30to call this function but we only needed
  5775. 4:11:32to set the name and HTTP only
  5776. 4:11:35parameters. If we want to set the HTTPON
  5777. 4:11:39parameter, we must provide values for
  5778. 4:11:41the other parameters, even if it's their
  5779. 4:11:44default values by using named arguments.
  5780. 4:11:47We can skip setting values for the other
  5781. 4:11:50parameters and only focus on what we
  5782. 4:11:52want to set. Named arguments improve
  5783. 4:11:55readability in our code by allowing us
  5784. 4:11:57to configure specific parameters and
  5785. 4:12:00forget the rest. So that's how named
  5786. 4:12:03arguments work. We'll be using them from
  5787. 4:12:05time to time in our projects.
  5788. 4:12:07[Music]
  5789. 4:12:19In this lecture, we're going to talk
  5790. 4:12:21about global variables. Before doing so,
  5791. 4:12:24we should talk about the concept of
  5792. 4:12:26scope. Scope affects the way we write
  5793. 4:12:28programs. So, what is scope? Scope
  5794. 4:12:31refers to the accessibility of your
  5795. 4:12:33variables based on their location. The
  5796. 4:12:36location of where your variables are
  5797. 4:12:38defined matters a lot. If you define a
  5798. 4:12:40variable from within a function, it'll
  5799. 4:12:42be inaccessible outside of the function.
  5800. 4:12:45Vice versa, variables defined outside of
  5801. 4:12:48a function are inaccessible from within
  5802. 4:12:50a function unless we use a keyword
  5803. 4:12:53called global. Let's look at an example
  5804. 4:12:56in our script. Let's replace the current
  5805. 4:12:59example with a variable called x with a
  5806. 4:13:02value of five.
  5807. 4:13:06By defining the variable outside of a
  5808. 4:13:08function, it's accessible everywhere.
  5809. 4:13:10This includes other files. For example,
  5810. 4:13:13let's open the example.php
  5811. 4:13:16file.
  5812. 4:13:19Inside this file, let's increment the x
  5813. 4:13:21variable with the increment operator.
  5814. 4:13:26Next, let's echo the variable.
  5815. 4:13:31Technically, this variable doesn't exist
  5816. 4:13:33within the file. Therefore, we'll get an
  5817. 4:13:36error if we visit the file directly.
  5818. 4:13:39However, it can become available if we
  5819. 4:13:41include this file from the index file.
  5820. 4:13:44Back in the index file, add the include
  5821. 4:13:46keyword to load the example.php
  5822. 4:13:49file.
  5823. 4:13:52Be sure to add this statement after the
  5824. 4:13:54variable has been created. If we look in
  5825. 4:13:57the preview, the number six has been
  5826. 4:13:59outputed. Despite the variable being
  5827. 4:14:02defined in another file, PHP exposes our
  5828. 4:14:05variables to other files, they just have
  5829. 4:14:08to be defined before they're accessed.
  5830. 4:14:10This is why we must include the file
  5831. 4:14:13after declaring the variable. Let's take
  5832. 4:14:15it a step further by using the function
  5833. 4:14:18from within a function. After the
  5834. 4:14:20include statement, define a function
  5835. 4:14:22called fu.
  5836. 4:14:25Inside this function, let's echo the x
  5837. 4:14:27variable.
  5838. 4:14:30Lastly, let's call the foo function.
  5839. 4:14:35After doing so, PHP throws an error at
  5840. 4:14:37us. It's saying that the X variable is
  5841. 4:14:40undefined, which is strange. However,
  5842. 4:14:42it's not strange once you consider
  5843. 4:14:44scope. Functions have a local scope.
  5844. 4:14:47They don't acknowledge variables defined
  5845. 4:14:49from outside the function. This is why
  5846. 4:14:51we're receiving an error. There is a
  5847. 4:14:53solution to override this behavior. At
  5848. 4:14:56the top of the function, we could
  5849. 4:14:57include the global keyword.
  5850. 4:15:01This keyword instructs PHP to look in
  5851. 4:15:04the global scope, which is where
  5852. 4:15:06variables exist when defined outside
  5853. 4:15:08functions. We can provide a list of
  5854. 4:15:10variables to grab from the global scope
  5855. 4:15:13by typing their name after this keyword.
  5856. 4:15:15Let's grab the X variable. If we had
  5857. 4:15:18multiple variables, we would separate
  5858. 4:15:20them with commas. After adding this
  5859. 4:15:22variable to the list, the error goes
  5860. 4:15:25away. One thing to keep in mind is that
  5861. 4:15:27the function can be modified. If we
  5862. 4:15:29modify it from within the function,
  5863. 4:15:31it'll affect other uses of the variable.
  5864. 4:15:34For example, let's use the increment
  5865. 4:15:36operator on the variable after echoing
  5866. 4:15:38the value.
  5867. 4:15:42Next, let's echo the variable one more
  5868. 4:15:44time. After calling the foo function
  5869. 4:15:48in the output, the number six gets
  5870. 4:15:51outputed twice because we're echoing the
  5871. 4:15:53value from within the example file and
  5872. 4:15:55foo function. Next, the numbers get
  5873. 4:15:58outputed one more time, but it's been
  5874. 4:16:00changed to seven because the value was
  5875. 4:16:02incremented from within the foo
  5876. 4:16:04function. A lot of developers don't like
  5877. 4:16:06global variables for this reason.
  5878. 4:16:09Variables that are accessible to every
  5879. 4:16:11file and function means that the value
  5880. 4:16:13can change at any time. Therefore,
  5881. 4:16:15variables become less reliable. But what
  5882. 4:16:18if we need to update a variable? The
  5883. 4:16:21recommended solution is to use
  5884. 4:16:23parameters and return values by passing
  5885. 4:16:26in the variable as an argument to the
  5886. 4:16:28function. The function will be able to
  5887. 4:16:30modify the value and then return the
  5888. 4:16:32result. The result can then be stored in
  5889. 4:16:35the variable. Throughout the course, I'm
  5890. 4:16:37going to be using parameters and return
  5891. 4:16:39values to update variables. Global
  5892. 4:16:42variables are not something I'm fond of
  5893. 4:16:44either. However, you may come across
  5894. 4:16:46global variables from time to time on
  5895. 4:16:49existing projects. They were popular
  5896. 4:16:51once upon a time ago. It's good to know
  5897. 4:16:53how they work. In the next lecture,
  5898. 4:16:55let's keep talking about variables.
  5899. 4:16:58[Music]
  5900. 4:17:10In this lecture, we're going to discuss
  5901. 4:17:12static variables. One thing to
  5902. 4:17:14understand about functions is that
  5903. 4:17:16variables are deleted after the function
  5904. 4:17:18finishes running. Let's look at an
  5905. 4:17:20example in our script. Replace the
  5906. 4:17:23current example with a function called
  5907. 4:17:25fu.
  5908. 4:17:28Inside this function, let's create a
  5909. 4:17:30variable called a with an initial value
  5910. 4:17:32of one.
  5911. 4:17:35Lastly, let's return the variable with
  5912. 4:17:38the increment operator.
  5913. 4:17:41After defining the function, let's call
  5914. 4:17:44it three times with a break element.
  5915. 4:17:50In the preview, the number appears three
  5916. 4:17:52times. However, it's the same number in
  5917. 4:17:55our function. The variable's initial
  5918. 4:17:57value is one. After the value gets
  5919. 4:17:59returned, the number should update by
  5920. 4:18:02one, resulting in the variable changing
  5921. 4:18:04its value to two. Here's the thing. PHP
  5922. 4:18:07destroys a variable after the function
  5923. 4:18:10is finished running. As a result,
  5924. 4:18:12calling the function again resets the
  5925. 4:18:14variable. This is why we're getting the
  5926. 4:18:16same output. This behavior is
  5927. 4:18:18beneficial. Most of the time, we don't
  5928. 4:18:20need the variable to see around after
  5929. 4:18:22the function is finished running. It's
  5930. 4:18:24nice that PHP performs some cleanup for
  5931. 4:18:27us. In some cases, you may want to keep
  5932. 4:18:30the variable around after the function
  5933. 4:18:32is finished running. This feature is
  5934. 4:18:34especially useful when you're writing
  5935. 4:18:36code that requires a lot of resources.
  5936. 4:18:39Creating variables can easily become an
  5937. 4:18:41expensive operation. The more data you
  5938. 4:18:44have, the longer it can take to run a
  5939. 4:18:46script. To avoid wasting resources, we
  5940. 4:18:49can use static variables. Static
  5941. 4:18:52variables are variables inside functions
  5942. 4:18:54that retain their value after the
  5943. 4:18:56function is finished running. We can
  5944. 4:18:58create a static variable by adding the
  5945. 4:19:01static keyword before the variable name
  5946. 4:19:03like so.
  5947. 4:19:06After adding this keyword, the output
  5948. 4:19:09changes. Instead of the same number, the
  5949. 4:19:11number slowly increments to three. Using
  5950. 4:19:14the static keyword is incredibly
  5951. 4:19:16powerful for keeping our programs
  5952. 4:19:18efficient. I'm sure you'll find it
  5953. 4:19:20useful. In the next lecture, let's get
  5954. 4:19:22into a discussion on anonymous
  5955. 4:19:24functions.
  5956. 4:19:26[Music]
  5957. 4:19:38In this lecture, we're going to explore
  5958. 4:19:40a strange topic that may be hard to wrap
  5959. 4:19:42your head around. I'll try my best to
  5960. 4:19:45explain it. So, what am I talking about?
  5961. 4:19:47I'm referring to a feature called
  5962. 4:19:49anonymous functions. An anonymous
  5963. 4:19:52function is just a function that doesn't
  5964. 4:19:54have a name. As always, let's look at an
  5965. 4:19:56example. In our script, replace the
  5966. 4:19:59contents with a function called
  5967. 4:20:01multiply.
  5968. 4:20:04This function will accept a parameter
  5969. 4:20:07called num.
  5970. 4:20:10Inside the function, let's return the
  5971. 4:20:12num parameter multiplied by two.
  5972. 4:20:17Lastly, let's invoke the function with a
  5973. 4:20:19random number.
  5974. 4:20:22The function is pretty straightforward.
  5975. 4:20:24We're multiplying a number by two.
  5976. 4:20:26Nothing fancy so far. Believe it or not,
  5977. 4:20:29PHP allows us to store a function from
  5978. 4:20:32within a variable. Doing so is very
  5979. 4:20:34simple. First, we must convert the
  5980. 4:20:36function into an anonymous function by
  5981. 4:20:39removing the name.
  5982. 4:20:42After removing the name, the preview
  5983. 4:20:44generates an error. The reason is
  5984. 4:20:46simple. Anonymous functions are
  5985. 4:20:48considered to be expressions. Like most
  5986. 4:20:51expressions, we must end the line of
  5987. 4:20:53code with a semicolon character.
  5988. 4:20:55Typically, this character can be omitted
  5989. 4:20:58when using curly brackets for blocks of
  5990. 4:21:00code. However, this is the exception to
  5991. 4:21:02the rule. Since we have an expression,
  5992. 4:21:05it's perfectly acceptable to store the
  5993. 4:21:07function from within a variable. Let's
  5994. 4:21:09set the function to a variable called
  5995. 4:21:12multiply.
  5996. 4:21:15We can invoke the function like any
  5997. 4:21:17other function. However, we must include
  5998. 4:21:19the dollar sign character at the
  5999. 4:21:21beginning of the function name. After
  6000. 4:21:23applying those changes, our code works
  6001. 4:21:26like before. We're able to reference a
  6002. 4:21:28function from a variable. But why would
  6003. 4:21:31we want to store a function in a
  6004. 4:21:32variable to begin with? There are two
  6005. 4:21:35advantages to doing so. Firstly, the
  6006. 4:21:37function can be swappable. Previously,
  6007. 4:21:40we weren't allowed to define a function
  6008. 4:21:42twice. If we had two functions with the
  6009. 4:21:45same name, PHP would throw an error.
  6010. 4:21:48However, since variables can have their
  6011. 4:21:50values updated, we can easily swap this
  6012. 4:21:53function with another function. For
  6013. 4:21:55example, after defining the multiply
  6014. 4:21:58variable, let's try updating its value
  6015. 4:22:00to another anonymous function. It's
  6016. 4:22:03going to have the same parameters and
  6017. 4:22:05return value. However, we're going to
  6018. 4:22:08multiply the num parameter by three
  6019. 4:22:10instead of two.
  6020. 4:22:14In the preview, the page outputs a
  6021. 4:22:17different value than before. This is
  6022. 4:22:19because it's using our new anonymous
  6023. 4:22:21function instead of the previous
  6024. 4:22:23function. Replacing functions is a
  6025. 4:22:25powerful feature. It allows us to alter
  6026. 4:22:28the behavior of our application at a
  6027. 4:22:30moment's notice. The second reason this
  6028. 4:22:32feature is useful is because it implies
  6029. 4:22:35that a function can be passed onto
  6030. 4:22:37another function as an argument. Let's
  6031. 4:22:40look at an example. First, I'm going to
  6032. 4:22:42remove the second anonymous function. It
  6033. 4:22:45isn't necessary anymore.
  6034. 4:22:49Next, define a function called sum. It's
  6035. 4:22:51a function we've defined before. It'll
  6036. 4:22:54accept two parameters called a and b.
  6037. 4:22:57Lastly, we'll return the result by
  6038. 4:23:00adding them together.
  6039. 4:23:03It's a pretty standard function. We can
  6040. 4:23:05expand on this function by allowing
  6041. 4:23:07developers to pass in an additional
  6042. 4:23:09function to modify the result.
  6043. 4:23:13In the parameter list, add a parameter
  6044. 4:23:16called callback.
  6045. 4:23:19Before going any further, let's talk
  6046. 4:23:22about the idea of a call back.
  6047. 4:23:26The idea of a call back is not specific
  6048. 4:23:28to PHP. It can be applied to various
  6049. 4:23:31programming languages. A call back is a
  6050. 4:23:33function passed into another function
  6051. 4:23:35that can be called later. It's like if
  6052. 4:23:38you gave your phone number to someone so
  6053. 4:23:39that they can call you at a later time.
  6054. 4:23:42It's the same idea, but with functions.
  6055. 4:23:45You're passing in a function that will
  6056. 4:23:46be called at a later time. Hence why
  6057. 4:23:49it's called a callback function.
  6058. 4:23:53If you ever define a function that
  6059. 4:23:55accepts a callback, it's standard
  6060. 4:23:57practice to name the parameter call
  6061. 4:23:59back. A lot of developers adopt this
  6062. 4:24:02practice. We'll be following it too.
  6063. 4:24:04After adding this parameter, let's wrap
  6064. 4:24:06our calculation with this function.
  6065. 4:24:11Next, let's update our echo statement to
  6066. 4:24:14pass in another function. It just so
  6067. 4:24:16happens that we have a function called
  6068. 4:24:18multiply that we can use.
  6069. 4:24:23Now, if we look at the page, a new
  6070. 4:24:25number gets produced. We're adding the
  6071. 4:24:27numbers passed into the function and
  6072. 4:24:29multiplying the result by two. Best of
  6073. 4:24:32all, we're able to swap callback
  6074. 4:24:34functions with different functions. One
  6075. 4:24:36moment we may want to multiply the
  6076. 4:24:38result, another moment we may want to
  6077. 4:24:40divide the results. Since we're not tied
  6078. 4:24:43to a specific function, we can easily
  6079. 4:24:45change the result without directly
  6080. 4:24:47modifying the original function. Using
  6081. 4:24:50anonymous functions gives us this power.
  6082. 4:24:53There's another advantage to using
  6083. 4:24:54anonymous functions. They can access
  6084. 4:24:57global variables without directly
  6085. 4:24:59modifying the original value. Let me
  6086. 4:25:02show you what I mean. Above the multiply
  6087. 4:25:04function, define a variable called
  6088. 4:25:06multiplier with an initial value of two.
  6089. 4:25:12Next, let's replace the number that gets
  6090. 4:25:14multiplied against the number parameter
  6091. 4:25:17with the multiplier variable.
  6092. 4:25:21Now, we have a variable for changing the
  6093. 4:25:24number that gets multiplied against the
  6094. 4:25:26parameter. This makes things more
  6095. 4:25:28flexible. However, as we know, variables
  6096. 4:25:31defined outside of the function can't be
  6097. 4:25:34used. One solution would be to use the
  6098. 4:25:36global keyword. But there's an even
  6099. 4:25:38better solution. After the parameter
  6100. 4:25:40list, add the use keyword.
  6101. 4:25:44The use keyword provides access to
  6102. 4:25:47variables outside of the function. We
  6103. 4:25:49can add a pair of parentheses followed
  6104. 4:25:51by a list of variables to grab separated
  6105. 4:25:54by commas. Let's include the multiplier
  6106. 4:25:56variable.
  6107. 4:25:59You may be wondering, how is this any
  6108. 4:26:01better? Here's the interesting thing.
  6109. 4:26:03PHP creates a copy of the variable
  6110. 4:26:06instead of directly giving us access to
  6111. 4:26:08the original variable. For example, in
  6112. 4:26:11the anonymous function, let's change the
  6113. 4:26:13multiplier variable to five.
  6114. 4:26:17If we look at the preview, the code is
  6115. 4:26:20working. Unlike before, the number is
  6116. 4:26:22even higher. This is because we're
  6117. 4:26:24modifying the multiplier variable to a
  6118. 4:26:27larger value. However, the multiplier
  6119. 4:26:29variable is just a copy of the original.
  6120. 4:26:32At the end of our code, let's add a line
  6121. 4:26:35break element and echo the multiplier
  6122. 4:26:37variable.
  6123. 4:26:40As you can see, the multiplier variable
  6124. 4:26:43retains its original value. We don't
  6125. 4:26:45suffer from the problems of a global
  6126. 4:26:47variable where the value does get
  6127. 4:26:49modified after the function finishes
  6128. 4:26:52running. It's much safer to use
  6129. 4:26:54anonymous functions. There's one last
  6130. 4:26:56thing I want to show you before moving
  6131. 4:26:58on. In PHP 7.4, Four arrow functions
  6132. 4:27:02were introduced. They're a shorter way
  6133. 4:27:04of writing anonymous functions. Let's
  6134. 4:27:06convert our anonymous function into an
  6135. 4:27:08arrow function. It's very easy to do.
  6136. 4:27:11First, change the keyword from function
  6137. 4:27:14to fn.
  6138. 4:27:17Next, replace the use keyword with a fat
  6139. 4:27:20arrow.
  6140. 4:27:23Fat arrows are written with equals
  6141. 4:27:25greater than characters. Afterward, we
  6142. 4:27:28can remove the curly brackets and return
  6143. 4:27:30keyword.
  6144. 4:27:33That leaves us with the expression.
  6145. 4:27:35Arrow functions are just one line of
  6146. 4:27:37code to the right of the arrow. The code
  6147. 4:27:40must be an expression. This is because
  6148. 4:27:42arrow functions must always return a
  6149. 4:27:44value. We don't have to include the
  6150. 4:27:46return keyword because PHP already
  6151. 4:27:49returns the value. In addition, the use
  6152. 4:27:52keyword does not need to be included.
  6153. 4:27:54Unlike regular or the other version of
  6154. 4:27:57anonymous functions, arrow functions
  6155. 4:27:59have access to variables from the parent
  6156. 4:28:01scope. If we look at the preview, we get
  6157. 4:28:04the same result as before. Arrow
  6158. 4:28:06functions are shorter and easier to
  6159. 4:28:08write, but there are some caveats.
  6160. 4:28:10Firstly, we must always return a value.
  6161. 4:28:13Anonymous functions don't have to return
  6162. 4:28:15a value, but arrow functions do.
  6163. 4:28:18Secondly, arrow functions can only
  6164. 4:28:20contain an expression. We can't add
  6165. 4:28:22curly brackets to add additional logic.
  6166. 4:28:25We're restricted to a single line of
  6167. 4:28:26code. As long as you keep these points
  6168. 4:28:29in mind, I think you'll be fine. In the
  6169. 4:28:31next lecture, let's keep talking about
  6170. 4:28:33functions.
  6171. 4:28:35[Music]
  6172. 4:28:46In this lecture, let's talk about the
  6173. 4:28:48callable type, which is a type we
  6174. 4:28:50avoided during our discussion of data
  6175. 4:28:52types. The callable data type is a data
  6176. 4:28:55type you can use for callback functions.
  6177. 4:28:57Let's keep working on our example from
  6178. 4:29:00the previous lecture. We have a function
  6179. 4:29:02called sum with three parameters. The
  6180. 4:29:04first two parameters can be annotated
  6181. 4:29:06with the integer and float types as a
  6182. 4:29:09union type.
  6183. 4:29:12But what about the callback parameter?
  6184. 4:29:15In this case, we can use the callable
  6185. 4:29:17type.
  6186. 4:29:19By using this type, we're telling PHP
  6187. 4:29:22that this parameter will store a
  6188. 4:29:24function. Looking at the preview,
  6189. 4:29:26everything is working as it did before.
  6190. 4:29:28There are different ways of passing in a
  6191. 4:29:30function. The first approach is to store
  6192. 4:29:32the function in a variable and then pass
  6193. 4:29:34it in like we're doing currently.
  6194. 4:29:36Another way of doing it is by just
  6195. 4:29:38passing the function directly from the
  6196. 4:29:40function invocation. For example, let's
  6197. 4:29:43copy and paste the value of the multiply
  6198. 4:29:46function into the sum function like so.
  6199. 4:29:51This time we're directly passing on the
  6200. 4:29:53function. It's common practice to write
  6201. 4:29:55the function directly from within the
  6202. 4:29:57function call. If you don't plan on
  6203. 4:29:59using the function elsewhere, it can be
  6204. 4:30:01convenient to just write it here. In the
  6205. 4:30:04preview, our code is still working. The
  6206. 4:30:06last approach would be to just pass in a
  6207. 4:30:09regular function. Regular functions are
  6208. 4:30:11accepted, too. Below the multiply
  6209. 4:30:13variable, let's define a regular
  6210. 4:30:15function called another multiply. It's
  6211. 4:30:18going to have the exact same logic as
  6212. 4:30:20the first multiply function.
  6213. 4:30:25In this example, I'm not using the
  6214. 4:30:27multiplier variable for the sake of
  6215. 4:30:29simplicity. Next, in the echo
  6216. 4:30:31statements, we can replace the arrow
  6217. 4:30:33function with a string that contains the
  6218. 4:30:35name of the another multiply function.
  6219. 4:30:40If we plan on using a regular function,
  6220. 4:30:42we can pass in the name as a string. PHP
  6221. 4:30:45handles finding and calling the correct
  6222. 4:30:48function. In the preview, everything is
  6223. 4:30:50working as it did before. Overall, the
  6224. 4:30:53callable type is the correct type for
  6225. 4:30:55parameters that store functions. In the
  6226. 4:30:57next lecture, let's get into a
  6227. 4:30:59discussion on references and values.
  6228. 4:31:02[Music]
  6229. 4:31:14In this lecture, we're going to learn
  6230. 4:31:16how to pass on a value by reference.
  6231. 4:31:18This concept tends to be confusing for
  6232. 4:31:21beginners. It can be difficult to wrap
  6233. 4:31:23your head around. Before we get into a
  6234. 4:31:25code example, let's look at a common
  6235. 4:31:27analogy that I think perfectly explains
  6236. 4:31:30passing by references. First, let's talk
  6237. 4:31:33about how a regular parameter works.
  6238. 4:31:35Throughout this course, we've defined a
  6239. 4:31:38few functions with parameters. Every
  6240. 4:31:40time we pass a value to the function,
  6241. 4:31:42PHP creates a copy of the value that
  6242. 4:31:45gets stored in the parameter. If we
  6243. 4:31:47modify the parameter, only the
  6244. 4:31:49parameter's copy of the value gets
  6245. 4:31:51modified. The original value remains
  6246. 4:31:54unaffected. In this example, let's
  6247. 4:31:56imagine we had a variable called cup
  6248. 4:31:59defined outside of a function. Next, we
  6249. 4:32:02may have a function called fill cup. If
  6250. 4:32:04we pass the cup variable to this
  6251. 4:32:06function, the fill cup function is going
  6252. 4:32:09to store a copy of the cup. If the
  6253. 4:32:11function fills the cup with coffee, only
  6254. 4:32:14the copy gets affected. the original cup
  6255. 4:32:16variable remains empty. This is the
  6256. 4:32:19default behavior for parameters. We
  6257. 4:32:21refer to this behavior as pass by value.
  6258. 4:32:25On the other hand, pass by reference
  6259. 4:32:27works differently. Passing a variable by
  6260. 4:32:30reference changes the default behavior.
  6261. 4:32:32Instead of creating a copy of a
  6262. 4:32:34variable, we have direct access to the
  6263. 4:32:36original variable. In this example, the
  6264. 4:32:39cup variable gets passed into the fill
  6265. 4:32:42cup function. If the fill cup function
  6266. 4:32:44affects the cup variable, the original
  6267. 4:32:47copy gets affected. Hopefully, this
  6268. 4:32:50gives you a better idea of how passing
  6269. 4:32:52by reference and passing by value work.
  6270. 4:32:55Let's start looking at a code example
  6271. 4:32:59in our script. Let's replace the current
  6272. 4:33:01code with a fresh example. Actually,
  6273. 4:33:04let's try recreating the example from
  6274. 4:33:06the image I just showed you. Define a
  6275. 4:33:08variable called cup. Its initial value
  6276. 4:33:11will be a string that says empty.
  6277. 4:33:16Next, define a function called fill cup
  6278. 4:33:18with a parameter called cup parameter.
  6279. 4:33:23Inside the function, let's update the
  6280. 4:33:26cup parameter variable to filled.
  6281. 4:33:31After defining the function, call the
  6282. 4:33:33fill cup function with the cup variable.
  6283. 4:33:38Lastly, let's echo the cup variable.
  6284. 4:33:43As expected, the cup remains empty. This
  6285. 4:33:46is because the cup parameter variable is
  6286. 4:33:49a unique copy of our cup variable.
  6287. 4:33:51Modifying this parameter only affects
  6288. 4:33:54the variable inside the function. The
  6289. 4:33:56original cup variable retains its
  6290. 4:33:58original value. What if we don't want to
  6291. 4:34:01create a unique copy? We may want to
  6292. 4:34:03continue using the original copy. In
  6293. 4:34:06this case, we can add the amperand
  6294. 4:34:08character before the parameter name like
  6295. 4:34:11so.
  6296. 4:34:14This time, if we look at the preview,
  6297. 4:34:16the cup has been filled. PHP is no
  6298. 4:34:18longer creating a unique copy of the
  6299. 4:34:21variable. The function can directly
  6300. 4:34:23modify the original variable. So, you
  6301. 4:34:26may be wondering, how is this any better
  6302. 4:34:28than global variables? Isn't modifying
  6303. 4:34:31variables outside of a function bad? One
  6304. 4:34:34difference between global variables and
  6305. 4:34:37variable references is the flexibility
  6306. 4:34:39offered by references. Let's say that I
  6307. 4:34:42was out dining with friends. Each of my
  6308. 4:34:44friends can have a cup. So, I'll change
  6309. 4:34:47the variable name to my cup.
  6310. 4:34:51Next, I'll create another variable
  6311. 4:34:54called my friend's cup with the same
  6312. 4:34:56value.
  6313. 4:34:59Now, we have two cups in the function.
  6314. 4:35:02If we were to use global variables, I
  6315. 4:35:04would have to list out every cup that I
  6316. 4:35:06want to modify.
  6317. 4:35:10As you can imagine, this is not
  6318. 4:35:11scalable. What if we had 20 cups?
  6319. 4:35:14Listing each variable is not efficient.
  6320. 4:35:17Instead of using global variables,
  6321. 4:35:19references are much easier to work with.
  6322. 4:35:22We can call the function for each
  6323. 4:35:24variable. Even better, we don't have to
  6324. 4:35:26call the function for each cup. Some
  6325. 4:35:29cups can just remain unfilled.
  6326. 4:35:31Alternatively, we may have another
  6327. 4:35:33function for filling the cup with juice
  6328. 4:35:35instead of coffee. Overall, passing by
  6329. 4:35:38reference gives us the flexibility to
  6330. 4:35:40pass in any variable instead of passing
  6331. 4:35:43in a specific variable. Hopefully, that
  6332. 4:35:46makes sense. In the next lecture, let's
  6333. 4:35:48start using our newfound knowledge of
  6334. 4:35:51functions to talk about arrays.
  6335. 4:35:53[Music]
  6336. 4:36:05In this lecture, let's explore the
  6337. 4:36:07various functions for arrays. Arrays are
  6338. 4:36:10extremely popular in PHP development. If
  6339. 4:36:13you think about it, almost everything is
  6340. 4:36:15a collection of data. We can have a list
  6341. 4:36:18of posts, products, users, transactions,
  6342. 4:36:21etc. So many of the sites we visit
  6343. 4:36:24present at least one list. You're very
  6344. 4:36:26likely to work with arrays on almost
  6345. 4:36:28every page you create. In the resource
  6346. 4:36:31section of this lecture, I provide a
  6347. 4:36:33link to an official list of functions
  6348. 4:36:35for arrays. Arrays are so popular, PHP
  6349. 4:36:39offers dozens of functions for working
  6350. 4:36:41with them. Almost any type of action
  6351. 4:36:43you'd like to perform on an array can be
  6352. 4:36:46performed with PHP's functions. I said
  6353. 4:36:49this before, but it bears repeating. You
  6354. 4:36:51won't be able to memorize every function
  6355. 4:36:53offered by PHP. However, there are some
  6356. 4:36:57functions that you'll likely use more
  6357. 4:36:59than others. This lecture covers some of
  6358. 4:37:01the most common functions for working
  6359. 4:37:03with arrays. Let's get started. Switch
  6360. 4:37:06over to replet. Replace the current
  6361. 4:37:08codebase with a variable called users.
  6362. 4:37:12This variable will be an array of names.
  6363. 4:37:14I'll add the following names: John,
  6364. 4:37:17Jane, and Bob.
  6365. 4:37:20One of the most common scenarios is to
  6366. 4:37:23check for an existing item in an array.
  6367. 4:37:25As we know, PHP throws errors if we
  6368. 4:37:28attempt to access an item in an array
  6369. 4:37:31that doesn't exist. To avoid this issue,
  6370. 4:37:34it's common practice to check that an
  6371. 4:37:36item exists before doing anything else.
  6372. 4:37:39PHP offers two functions for this
  6373. 4:37:41scenario. The most common function to
  6374. 4:37:44use is called is set. Below the
  6375. 4:37:46variable, add a conditional statement
  6376. 4:37:48with the is set function.
  6377. 4:37:53This function checks that a variable has
  6378. 4:37:55a value. It's not exclusive to arrays.
  6379. 4:37:58We can use it to check for strings,
  6380. 4:38:00integers, floats, and booleans. Let's
  6381. 4:38:03check if the first item in the users
  6382. 4:38:05array has a value by passing in users
  6383. 4:38:08zero.
  6384. 4:38:11Inside the conditional block, let's echo
  6385. 4:38:14a message that says user found.
  6386. 4:38:18In the preview, the user was found. The
  6387. 4:38:21is set function returns a boolean value
  6388. 4:38:24based on whether the user could be
  6389. 4:38:26found. Everything works, but there is
  6390. 4:38:28one thing we need to be aware of. What
  6391. 4:38:31if we change the index from 0 to three?
  6392. 4:38:36As expected, nothing appears because
  6393. 4:38:38there isn't an item in the third index.
  6394. 4:38:41Let's try adding null to the array.
  6395. 4:38:45Despite adding a new item to the array,
  6396. 4:38:48a user hasn't been found. Technically,
  6397. 4:38:51there's an item in the third index.
  6398. 4:38:53However, the is set function considers
  6399. 4:38:56null and false values to be the same
  6400. 4:38:59thing as not having a value in that
  6401. 4:39:01index. You may disagree with that
  6402. 4:39:03behavior. For this reason, PHP
  6403. 4:39:06introduces a completely different
  6404. 4:39:08function for performing a similar task
  6405. 4:39:10as the is set function called array key
  6406. 4:39:14exists. Unlike the is set function, this
  6407. 4:39:17function is designed for arrays only.
  6408. 4:39:19Let's replace the condition with this
  6409. 4:39:21function.
  6410. 4:39:24This function has two parameters. The
  6411. 4:39:27first parameter is the index which can
  6412. 4:39:29be a string or integer. Let's pass in
  6413. 4:39:32three. Next, we must pass in the array
  6414. 4:39:34itself. Let's pass in the user's array.
  6415. 4:39:39This time, the user has been found. The
  6416. 4:39:42array key exists function allows for
  6417. 4:39:45null or false values. As long as there's
  6418. 4:39:48a value in a specific index, the
  6419. 4:39:50function returns true. Let's move on to
  6420. 4:39:53another function. In our array, we have
  6421. 4:39:55a null value. It's not uncommon to have
  6422. 4:39:58values in your array that you don't want
  6423. 4:40:00to keep around. PHP has a function
  6424. 4:40:03called array filter for filtering items
  6425. 4:40:06out of an array. Let's replace the
  6426. 4:40:08conditional statement with a
  6427. 4:40:10reassignment of the users variable. Set
  6428. 4:40:12the variable to the array filter
  6429. 4:40:15function.
  6430. 4:40:18This function accepts the array to
  6431. 4:40:20filter. Let's pass in the user's array.
  6432. 4:40:25Lastly, let's print the contents of the
  6433. 4:40:27array by using the print r function.
  6434. 4:40:32For a cleaner output, I recommend
  6435. 4:40:34echoing pre-tags around the print r
  6436. 4:40:37function like so.
  6437. 4:40:41They're incredibly useful for formatting
  6438. 4:40:43arrays onto the page instead of the
  6439. 4:40:45entire array being printed on a single
  6440. 4:40:48line. Each item gets put on a single
  6441. 4:40:50line. Looking closely, the null value is
  6442. 4:40:53absent from the array. By default, the
  6443. 4:40:56array filter function removes items with
  6444. 4:40:59empty values like null or false.
  6445. 4:41:01However, we may want to customize this
  6446. 4:41:04behavior. In the resource section of
  6447. 4:41:06this lecture, I provide a link to this
  6448. 4:41:08function. If we look at the function
  6449. 4:41:10definition, this function has a few
  6450. 4:41:12parameters. I want to focus on the
  6451. 4:41:14second parameter. Let's take a close
  6452. 4:41:17look at the data type. It's set to
  6453. 4:41:19callable. As we know, the callable data
  6454. 4:41:22type is a data type for callback
  6455. 4:41:24functions. PHP uses callback functions
  6456. 4:41:28to allow other developers to customize
  6457. 4:41:30the behavior of its own functions. If
  6458. 4:41:33you were to read through the
  6459. 4:41:34documentation, the callback function can
  6460. 4:41:37be used for customizing the filtering
  6461. 4:41:39behavior. Let's give it a try. Back in
  6462. 4:41:42our script, pass in an arrow function as
  6463. 4:41:44the second argument to the array filter
  6464. 4:41:47function.
  6465. 4:41:50In this example, let's try removing Bob
  6466. 4:41:52from the array. In some cases, we may
  6467. 4:41:55want to exclude specific users. So,
  6468. 4:41:58filtering a specific user isn't out of
  6469. 4:42:00the question. Behind the scenes, PHP
  6470. 4:42:03loops through our array. Our callback
  6471. 4:42:05function will be called on each
  6472. 4:42:07iteration during which we can accept the
  6473. 4:42:10user as a parameter. Let's call the
  6474. 4:42:13parameter user.
  6475. 4:42:16Our callback function must return a
  6476. 4:42:18boolean value. If true is returned, the
  6477. 4:42:22item stays in the array, otherwise it'll
  6478. 4:42:25get removed. Let's return the following.
  6479. 4:42:27User does not equal Bob.
  6480. 4:42:32As long as the value is not equal to
  6481. 4:42:34Bob, the value can stay in the array.
  6482. 4:42:36Looking at the preview, Bob was
  6483. 4:42:38successfully removed. One thing you'll
  6484. 4:42:41notice is that the index between 1 and
  6485. 4:42:44three is skipped. BHP does not reindex
  6486. 4:42:47the array. As a mini exercise, I'll let
  6487. 4:42:50you try to fix this problem. We already
  6488. 4:42:52talked about a function that can be used
  6489. 4:42:54for reindexing values. Let's move on to
  6490. 4:42:58another function. Sometimes we may not
  6491. 4:43:00want to filter values. However, we may
  6492. 4:43:03want to make adjustments to each value
  6493. 4:43:05in an array. The array map function
  6494. 4:43:07allows us to do just that. Let's replace
  6495. 4:43:10the array filter function with the array
  6496. 4:43:13map function.
  6497. 4:43:16Next, the array map function accepts the
  6498. 4:43:19array as the second argument. So, let's
  6499. 4:43:22swap the parameters by moving the user's
  6500. 4:43:25array as the second argument.
  6501. 4:43:29Afterward, change the expression to the
  6502. 4:43:32following string to upper user.
  6503. 4:43:36Similar to the array filter function,
  6504. 4:43:39the array map function accepts a
  6505. 4:43:41callback function. Every value in the
  6506. 4:43:43array will be looped through. On each
  6507. 4:43:45iteration, our callback function will be
  6508. 4:43:48called with each value in the array. We
  6509. 4:43:50can use this opportunity to modify the
  6510. 4:43:53value. In this example, we're using a
  6511. 4:43:55new function called string to upper.
  6512. 4:43:58It'll convert lowercase letters into
  6513. 4:44:00uppercase. In the preview, the names
  6514. 4:44:02have been updated to all uppercase
  6515. 4:44:05letters. Perfect. We're able to modify
  6516. 4:44:07the values instead of filtering them. In
  6517. 4:44:10some scenarios, we may want to merge two
  6518. 4:44:13or more arrays. For example, let's say
  6519. 4:44:16we were creating an application for
  6520. 4:44:18tracking expenses. You may allow users
  6521. 4:44:20to upload documents with their expense
  6522. 4:44:23reports. Since multiple reports can be
  6523. 4:44:25uploaded, you may have multiple arrays
  6524. 4:44:28of data. Dealing with multiple arrays
  6525. 4:44:30can be a hassle, so merging them into a
  6526. 4:44:33single array is the best way to go. PHP
  6527. 4:44:36offers a function for merging arrays
  6528. 4:44:38called array merge. Let's replace the
  6529. 4:44:41array map function with this function.
  6530. 4:44:46Next, we must pass in our arrays. In the
  6531. 4:44:49resource section of this lecture, I
  6532. 4:44:51provide a link to the array merge
  6533. 4:44:53function. Take a close look at the
  6534. 4:44:55definition. Notice anything significant?
  6535. 4:44:58The array merge function is a variatic
  6536. 4:45:01function. It has a single parameter with
  6537. 4:45:04the spread operator, meaning that the
  6538. 4:45:06function can accept an unlimited number
  6539. 4:45:08of arrays. Let's head back to our
  6540. 4:45:11editors. Passed in the users array.
  6541. 4:45:16Next, let's create an array on the spot.
  6542. 4:45:19In this array, insert two users called
  6543. 4:45:21Sam and Jessica.
  6544. 4:45:25In the preview, Sam and Jessica have
  6545. 4:45:27been added. Perfect. There's one last
  6546. 4:45:30thing I want to show you. Sorting arrays
  6547. 4:45:32is another task we may want to perform.
  6548. 4:45:34In the resource section of this lecture,
  6549. 4:45:37I provide a link to a function called
  6550. 4:45:39sort. The sort function does exactly as
  6551. 4:45:42it sounds. It'll sort the contents of an
  6552. 4:45:45array. Before using this function,
  6553. 4:45:47there's one thing worth highlighting.
  6554. 4:45:49Thus far, every function has returned a
  6555. 4:45:52brand new array. This time, the function
  6556. 4:45:54definition tells us otherwise. Unlike
  6557. 4:45:57before, this function returns a boolean
  6558. 4:46:00value. So, how are we expected to grab
  6559. 4:46:02the sorted array? Taking another look,
  6560. 4:46:05the first parameter gives us our answer.
  6561. 4:46:08There's an amperand character before the
  6562. 4:46:10name. This character tells us that the
  6563. 4:46:13parameter accepts a reference to the
  6564. 4:46:15variable. We can safely assume that this
  6565. 4:46:18function modifies an existing array
  6566. 4:46:20directly. It does not store a copy of
  6567. 4:46:23our array. This is important to
  6568. 4:46:25understand. Not all of PHP's functions
  6569. 4:46:28return a new value. Some of them may
  6570. 4:46:30want to modify an existing variable. The
  6571. 4:46:33only way to know is to read the
  6572. 4:46:35documentation and function definition.
  6573. 4:46:37Let's give this function a try. Back in
  6574. 4:46:40our script, call the sort function after
  6575. 4:46:42merging the arrays.
  6576. 4:46:46Pass in the users array.
  6577. 4:46:50In the preview, the array has been
  6578. 4:46:52sorted alphabetically. In addition, the
  6579. 4:46:54names can be found under new indexes.
  6580. 4:46:57Previously, Jessica's index was five
  6581. 4:47:00because she was the last name to appear
  6582. 4:47:02in the array. This time, her index is
  6583. 4:47:05three. Not only were the values
  6584. 4:47:07rearranged, but the indexes were
  6585. 4:47:09affected, too. You may not find this
  6586. 4:47:12behavior desirable. What if we had an
  6587. 4:47:15associative array? Our named indexes
  6588. 4:47:17would be thrown away. In the resource
  6589. 4:47:20section of this lecture, I provide a
  6590. 4:47:22link to a documentation page with an
  6591. 4:47:24official list of sorting functions.
  6592. 4:47:26Sorting is a common action. PHP does not
  6593. 4:47:29assume how you want to sort your values.
  6594. 4:47:32For this reason, dozens of functions are
  6595. 4:47:34offered for sorting arrays differently.
  6596. 4:47:37If we scroll through the page, we'll
  6597. 4:47:39call across a table of these functions.
  6598. 4:47:42Some functions can sort by using the
  6599. 4:47:44value or the key.
  6600. 4:47:47Other functions can maintain the
  6601. 4:47:49original key of the value or reindex the
  6602. 4:47:52array.
  6603. 4:47:55Lastly, functions can sort in ascending
  6604. 4:47:58or descending order.
  6605. 4:48:01If a function doesn't satisfy what
  6606. 4:48:03you're looking for, PHP offers an
  6607. 4:48:05alternative solution.
  6608. 4:48:09We're looking for a function that sorts
  6609. 4:48:11an array by its value in ascending order
  6610. 4:48:14while maintaining the original key.
  6611. 4:48:16According to the table, the a sort
  6612. 4:48:19function seems like a perfect fit. Keep
  6613. 4:48:21in mind, the table only provides a
  6614. 4:48:23summary of what each function does. I
  6615. 4:48:26highly recommend checking out the
  6616. 4:48:28individual documentation pages for each
  6617. 4:48:30function to truly understand what it can
  6618. 4:48:33and can't do. Let's try using the a sort
  6619. 4:48:37function. Back in the script, replace
  6620. 4:48:39the sort function with the a sort
  6621. 4:48:42function.
  6622. 4:48:45The preview generates the same array as
  6623. 4:48:47before. The names are sorted
  6624. 4:48:49alphabetically. The main difference is
  6625. 4:48:51the keys. Jessica retains her original
  6626. 4:48:54index which was five. Everything is
  6627. 4:48:56working as intended. Hopefully, this
  6628. 4:48:59gives you an idea of what can be done
  6629. 4:49:01with arrays. We can't cover every
  6630. 4:49:03function, but we can go over the most
  6631. 4:49:06common ones. As we progress through the
  6632. 4:49:08course, I'll be sure to introduce
  6633. 4:49:10additional functions. For now, these
  6634. 4:49:13functions should suffice. In the next
  6635. 4:49:15lecture, let's explore another feature
  6636. 4:49:17of arrays.
  6637. 4:49:19[Music]
  6638. 4:49:31In this lecture, we're going to explore
  6639. 4:49:33a feature to help us improve
  6640. 4:49:35readability. Arrays can have dozens of
  6641. 4:49:37values. It's common to extract a single
  6642. 4:49:40value from an array and store it in a
  6643. 4:49:43variable. This is known as dstructuring.
  6644. 4:49:46Let's look at an example. In our script,
  6645. 4:49:49let's replace the current code with an
  6646. 4:49:51array called numbers. Fill the array
  6647. 4:49:53with random numbers.
  6648. 4:49:57Next, let's write the following. List a
  6649. 4:50:00equals numbers.
  6650. 4:50:03The list keyword can be used for
  6651. 4:50:05grabbing items from an array. PHP will
  6652. 4:50:08store them with the variable names
  6653. 4:50:10written inside the list keyword. The
  6654. 4:50:12order does matter. The first variable
  6655. 4:50:14gets assigned the first value in the
  6656. 4:50:17array. We can add more variables by
  6657. 4:50:19separating them with commas. For
  6658. 4:50:21example, I'll add a variable called B.
  6659. 4:50:24This variable will store the second
  6660. 4:50:26value in the array. so on and so forth.
  6661. 4:50:29It's not required to provide a variable
  6662. 4:50:32for each item. Let's echo the a
  6663. 4:50:34variable.
  6664. 4:50:37As you can see, we get the first number
  6665. 4:50:39from the array. There's an alternative
  6666. 4:50:41syntax for dstructuring an array. Rather
  6667. 4:50:44than wrapping the variable with the list
  6668. 4:50:46keyword, we can use a pair of square
  6669. 4:50:48brackets.
  6670. 4:50:51This shorthand syntax produces the same
  6671. 4:50:54results. The last thing I want to show
  6672. 4:50:56you is dstructuring arrays with key
  6673. 4:50:59names. Let's update the array by setting
  6674. 4:51:01the index of the first variable to
  6675. 4:51:04example.
  6676. 4:51:06Next, in our dstructuring, we can grab
  6677. 4:51:09the variable by referencing the key name
  6678. 4:51:12followed by a fat arrow and variable
  6679. 4:51:14name.
  6680. 4:51:17If we decide to dstructure variables by
  6681. 4:51:20specific keys, all variables in the
  6682. 4:51:23dstructuring must also use specific
  6683. 4:51:25keys. So for the B variable, set the key
  6684. 4:51:29to zero.
  6685. 4:51:32In the preview, the variable continues
  6686. 4:51:34to work. Dstructuring can be handy
  6687. 4:51:37because it allows us to assign a
  6688. 4:51:39specific variable name to a value in an
  6689. 4:51:42array. It makes our code readable and
  6690. 4:51:44concise. In the next lecture, let's
  6691. 4:51:47discuss one final topic for this
  6692. 4:51:49section.
  6693. 4:51:50[Music]
  6694. 4:52:02In this lecture, let's talk about the
  6695. 4:52:04file system. In the resource section of
  6696. 4:52:07this lecture, I provide a link to PHP's
  6697. 4:52:10official list of file system functions.
  6698. 4:52:12PHP offers dozens of functions for
  6699. 4:52:15interacting with the file system. You
  6700. 4:52:17can perform operations from reading the
  6701. 4:52:20file size to creating new files. Let's
  6702. 4:52:22give some of these functions a try.
  6703. 4:52:25Switch over to replet. Replace the
  6704. 4:52:27contents of the file with a variable
  6705. 4:52:29called directory. Its value will be a
  6706. 4:52:32function called scan directory.
  6707. 4:52:36Typically, you're going to want to scan
  6708. 4:52:39a directory for a list of files. The
  6709. 4:52:41scan directory function performs this
  6710. 4:52:44exact action. There's one argument,
  6711. 4:52:46which is the directory to scan. Luckily,
  6712. 4:52:49we don't have to guess the directory
  6713. 4:52:51we're currently in. We can use the
  6714. 4:52:53directory magic constant we learned
  6715. 4:52:55about in an earlier lecture.
  6716. 4:52:59As a reminder, this constant points to
  6717. 4:53:02the directory of the file it's being
  6718. 4:53:04used in. The return value of this
  6719. 4:53:07function is an array. So let's print the
  6720. 4:53:10contents of the directory variable with
  6721. 4:53:12the print r function.
  6722. 4:53:16In the preview, we're given a list of
  6723. 4:53:18files. You should see most of our files,
  6724. 4:53:21including the hidden files created by
  6725. 4:53:23replet. There are two items in the array
  6726. 4:53:26that may be mysterious. Looking closely,
  6727. 4:53:29the first two items of the array are a
  6728. 4:53:31dot and two dots. Every time you scan a
  6729. 4:53:34directory, every directory is going to
  6730. 4:53:37have these two items. A single dot
  6731. 4:53:39refers to the current directory, whereas
  6732. 4:53:41the two dots refer to the parent
  6733. 4:53:44directory. Keep this in mind as you're
  6734. 4:53:46not just going to be presented with the
  6735. 4:53:48files in a directory. These items will
  6736. 4:53:51always appear in your array. Moving
  6737. 4:53:53along, let's try creating a new
  6738. 4:53:55directory. Replace the current code with
  6739. 4:53:58a function called mk directory.
  6740. 4:54:03The letters mk are short for make. This
  6741. 4:54:06function accepts the name of the
  6742. 4:54:08directory as a string. Let's set the
  6743. 4:54:10name to fu.
  6744. 4:54:14If we run the file, nothing appears in
  6745. 4:54:16the preview. However, if we look at the
  6746. 4:54:19file tree from the sidebar, a new folder
  6747. 4:54:22has been created called fu. Next, let's
  6748. 4:54:25try replacing the function with a
  6749. 4:54:27function called rm directory.
  6750. 4:54:32RM being short for remove. After
  6751. 4:54:35refreshing the page, the food directory
  6752. 4:54:37disappears. We can use this function for
  6753. 4:54:40deleting directories. There is one thing
  6754. 4:54:42to keep in mind when deleting
  6755. 4:54:44directories. The directory must be empty
  6756. 4:54:46before being deleted. If we attempt to
  6757. 4:54:49delete a directory with files, PHP
  6758. 4:54:51throws an error. So keep that in mind
  6759. 4:54:54when deleting directories. Let's start
  6760. 4:54:56exploring functions for interacting with
  6761. 4:54:59files. Before working with a file, you
  6762. 4:55:01should check that it exists. Otherwise,
  6763. 4:55:04any action you perform may not work as
  6764. 4:55:07expected. In our script, replace the
  6765. 4:55:09function with a conditional statement.
  6766. 4:55:12The condition will be a function called
  6767. 4:55:14file exists.
  6768. 4:55:17The file exists function can be used to
  6769. 4:55:20check for the existence of a file. We
  6770. 4:55:23must pass in the name of the file as a
  6771. 4:55:25string. Unlike before, we don't need to
  6772. 4:55:28provide a full system path. It's
  6773. 4:55:30perfectly fine to write a path relative
  6774. 4:55:32to the current directory. For example,
  6775. 4:55:35let's try checking if a file called
  6776. 4:55:38example.txt
  6777. 4:55:40exists.
  6778. 4:55:42If it does, we'll echo a message saying
  6779. 4:55:45file found
  6780. 4:55:49in the preview. Nothing appears because
  6781. 4:55:51we don't have a file called example.txt.
  6782. 4:55:54Let's try creating this file from within
  6783. 4:55:57our project. Make sure it's in the same
  6784. 4:55:59directory as the index.php file.
  6785. 4:56:04Next, if we refresh the page, the
  6786. 4:56:07message appears. Great. We're able to
  6787. 4:56:10verify that a file exists. Let's try
  6788. 4:56:13checking the file size. Replace the
  6789. 4:56:15expression for the echo statement with
  6790. 4:56:17the file size function.
  6791. 4:56:21As you might expect, this function
  6792. 4:56:23returns the size of a file. Let's pass
  6793. 4:56:26in the example.txt
  6794. 4:56:28file.
  6795. 4:56:31In the preview, the number zero gets
  6796. 4:56:33rendered. That's to be expected since I
  6797. 4:56:36didn't write anything inside the text
  6798. 4:56:38file. Let's try writing content into the
  6799. 4:56:41file. PHP has a simple function to
  6800. 4:56:44perform this task. After the echo
  6801. 4:56:46statement, run a function called fileput
  6802. 4:56:49contents.
  6803. 4:56:52Two pieces of information must be passed
  6804. 4:56:54in. Firstly, we must provide the file
  6805. 4:56:57name, which is example.txt.
  6806. 4:57:02Next, we must provide the contents we'd
  6807. 4:57:05like to insert into the file. Let's
  6808. 4:57:07write hello world.
  6809. 4:57:10Lastly, just to verify that our function
  6810. 4:57:13worked, let's echo the file size again.
  6811. 4:57:18If we refresh the page, everything seems
  6812. 4:57:21to have worked. But there's just one
  6813. 4:57:23problem. The number zero gets outputed
  6814. 4:57:25twice. At first glance, it can seem like
  6815. 4:57:28PHP didn't update the example.php file.
  6816. 4:57:32Let's take a peek inside the example.txt
  6817. 4:57:35file. Surprisingly, the value has passed
  6818. 4:57:38into our function. So why is PHP saying
  6819. 4:57:41that the file size is zero bytes? The
  6820. 4:57:44reason is simple. Behind the scenes, PHP
  6821. 4:57:47caches the results returned by some of
  6822. 4:57:50the file system functions. By calling
  6823. 4:57:52the file size function before updating
  6824. 4:57:55the files contents, the result gets
  6825. 4:57:57cached. If we attempt to call the file
  6826. 4:58:00size function again, we're going to get
  6827. 4:58:02the previous result. PHP does not bother
  6828. 4:58:06rechecking the file for a new file size.
  6829. 4:58:08Checking file data can be an expensive
  6830. 4:58:11action. For performance reasons, PHP
  6831. 4:58:14reuses the original result. This
  6832. 4:58:16behavior is beneficial to us, but in
  6833. 4:58:19this case, it's a problem. Luckily, we
  6834. 4:58:21can reset the results in the text file.
  6835. 4:58:24Clear the contents.
  6836. 4:58:28Back in the PHP file, after updating the
  6837. 4:58:30files contents, call the clear stat
  6838. 4:58:33cache function.
  6839. 4:58:37The clear stat cache function clears the
  6840. 4:58:40cache. Therefore, PHP attempts to reread
  6841. 4:58:43the file size. In the preview, zero gets
  6842. 4:58:46rendered in the output, followed by 11,
  6843. 4:58:49thus indicating that the file size was
  6844. 4:58:51updated. Perfect. This is something to
  6845. 4:58:54keep in mind. If you're constantly
  6846. 4:58:56reading file data, you may be returned
  6847. 4:58:59older results instead of newer results.
  6848. 4:59:01You can avoid this behavior by calling
  6849. 4:59:04the clear stat cache function. Let's
  6850. 4:59:07explore one more function. Instead of
  6851. 4:59:09outputting file size, let's grab the
  6852. 4:59:11contents of the file. For the second
  6853. 4:59:14echo statement, replace the file size
  6854. 4:59:16function with the file get contents
  6855. 4:59:19function.
  6856. 4:59:22In the preview, the text from the file
  6857. 4:59:24appears. It's as simple as that. The
  6858. 4:59:27file get contents function just accepts
  6859. 4:59:30the name of the file. It'll return the
  6860. 4:59:32file contents as a string. Hopefully,
  6861. 4:59:35this gives you a better idea of how we
  6862. 4:59:37can work with files in PHP. That
  6863. 4:59:40concludes this section. We've explored
  6864. 4:59:43dozens of PHP's features. Now that we
  6865. 4:59:46have a solid grasp of PHP, it's time to
  6866. 4:59:49take things to the next level. We're
  6867. 4:59:51going to start talking about
  6868. 4:59:52objectoriented programming. Before we
  6869. 4:59:55do, I have a few challenges for you to
  6870. 4:59:57take on to reinforce what we've learned.
  6871. 5:00:00So, when you're ready, I'll see you in
  6872. 5:00:02the next section.
  6873. 5:00:03[Music]
  6874. 5:00:14Nice job. You've just taken your first
  6875. 5:00:15steps into PHP development. You now know
  6876. 5:00:18how to use variables and data, write
  6877. 5:00:20logic to control your code, use the
  6878. 5:00:22official PHP documentation to keep
  6879. 5:00:24learning on your own. These are the
  6880. 5:00:26foundations that you need to start
  6881. 5:00:27building realworld projects. But PHP
  6882. 5:00:30goes much deeper than that. So, if
  6883. 5:00:32you're ready to start diving into OOP,
  6884. 5:00:34authentication, databases, and even
  6885. 5:00:36build a full expense tracking app, then
  6886. 5:00:38check out Louisa's full PHP boot camp
  6887. 5:00:40course. It's designed to take you from
  6888. 5:00:42beginner to building professional-grade
  6889. 5:00:44applications that you can put on your
  6890. 5:00:46portfolio and will help you stand out to
  6891. 5:00:48land more interviews and get hired. And
  6892. 5:00:51if you decide to join the Zero to
  6893. 5:00:52Mastery Academy, you'll not only get
  6894. 5:00:54full access to Louisa's full PHP boot
  6895. 5:00:56camp course, but every single other
  6896. 5:00:58course that ZTM has to offer. and you'll
  6897. 5:01:01get access to our membersonly Discord
  6898. 5:01:03channel where you can get 247 priority
  6899. 5:01:05support to help you reach your goals
  6900. 5:01:07faster. If you've made it this far,
  6901. 5:01:09please help me show Louise some love by
  6902. 5:01:11dropping this video a like and leaving a
  6903. 5:01:13comment with your questions, thoughts,
  6904. 5:01:14or feedback down below. That's it for me
  6905. 5:01:17and I look forward to seeing you inside
  6906. 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.