Cara menggunakan goto in function php
Today, we're going to discuss control structures and loops in PHP. I'll show you how to use all the main control structures that are supported in PHP, like if, else, for, foreach, while, and more. Show What Is a Control Structure?In simple terms, a control structure allows you to control the flow of code execution in your application. Generally, a program is executed sequentially, line by line, and a control structure allows you to alter that flow, usually depending on certain conditions. Control structures are core features of the PHP language that allow your script to respond differently to different inputs or situations. This could allow your script to give different responses based on user input, file contents, or some other data. The following flowchart explains how a control structure works in PHP. As you can see in the above diagram, first a condition is checked. If the condition is true, the conditional code will be executed. The important thing to note here is that code execution continues normally after conditional code execution. Let's consider the following example. In the above example, the program checks whether or not the user is logged in. Based on the user's login status, they will be redirected to either the Login page or the My Account page. In this case, a control structure ends code execution by redirecting users to a different page. This is a crucial ability of the PHP language. PHP supports a number of different control structures:
Let's take a look at a few of these control structures with examples. Learning PHP Control StructuresIn the previous section, we learned the basics of control structures in PHP and their usefulness in application development. In this section, we'll go through a couple of important control structures that you'll end up using frequently in your day-to-day application development. PHP If StatementThe Let's have a look at the following example to understand how it actually works. 30) { echo "Your age is greater than 30!"; } ?> The above example should output the 30) echo "Your age is greater than 30!"; ?> On the other hand, if you have more than one statement to execute, you must use brackets, as shown in the following snippet. PHP Else StatementIn
the previous section, we discussed the You always use the if (expression) { // code is executed if the expression evaluates to TRUE } else { // code is executed if the expression evaluates to FALSE } Let's revise the previous example to understand how it works. So when you have two choices, and one of them must be executed, you can use the PHP Else If StatementWe can consider the Let's study the basic structure of the if (expression1) { // code is executed if the expression1 evaluates to TRUE } elseif (expression2) { // code is executed if the expression2 evaluates to TRUE } elseif (expression3) { // code is executed if the expression3 evaluates to TRUE } else { // code is executed if the expression1, expression2 and expression3 evaluates to FALSE, a default choice } Again, let's try to understand it using a real-world example. 30 && $age < 40) { echo "Your age is between 30 and 40!"; } elseif ($age > 40 && $age < 50) { echo "Your age is between 40 and 50!"; } else { echo "Your age is greater than 50!"; } ?> As you can see in the above example, we have multiple conditions, so we've used a series of PHP Switch StatementThe switch statement is somewhat similar to the In the case of the As usual, an
example is the best way to understand the As you can see in the above example, we want to check the value of the For each value you want to check with the Finally, if you want to execute a piece of code if the variable's value doesn't match any case, you can define it under the So that's the story of conditional control structures. We'll discuss loops in PHP in the next section. Loops in PHPLoops in PHP are useful when you want to execute a piece of code repeatedly until a condition evaluates to false. So code is executed repeatedly as long as a condition evaluates to true, and as soon as the condition evaluates to false, the script continues executing the code after the loop. The following flowchart explains how loops work in PHP. As you can see in the above screenshot, a loop contains a condition. If the condition evaluates to true, the conditional code is executed. After execution of the conditional code, control goes back to the loop condition, and the flow continues until the condition evaluates to false. In this section, we'll go through the different types of loops supported in PHP. While Loop in PHPThe You can define it as shown in the following pseudo-code. while (expression) { // code to execute as long as expression evaluates to TRUE } Let's have a look at a real-world example to
understand how the If you're familiar with the Fibonacci series, you might recognize what the above program does—it outputs the Fibonacci series for the first ten numbers. The Do-While Loop in PHPThe Let's have a look at the syntax of the do { // code to execute } while (expression); Let's go through a real-world to understand possible cases where you can use the In the above example, we're trying to read a file line by line. Firstly, we've opened a file for reading. In
our case, we're not sure if the file contains any content at all. Thus, we need to execute the For Loop in PHPGenerally, the Let's have a look at the syntax of the for (expr1; expr2; expr3) { // code to execute } The Let's go through the following example to see how it works. ", $i, $i*$i); } ?> The above program outputs the square of the first ten numbers. It initializes For Each in PHPThe Let's have a look at a couple of examples. "; } $employee = array('name' => 'John Smith', 'age' => 30, 'profession' => 'Software Engineer'); foreach ($employee as $key => $value) { echo sprintf("%s: %s", $key, $value); echo " If you want to access array values, you can use the first version of the Breaking Out of the LoopThere are times when you might want to break out of a loop before it runs its course. This can be achieved easily using the You can also use Another keyword that can interrupt loops in PHP is ConclusionIn this article, we discussed different control structures and loops in PHP. They are an essential part of PHP—or any programming language for that matter. Learn PHP With a Free Online CourseIf you want to learn PHP, check out our free online course on PHP fundamentals! In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database. Did you find this post useful? Software Engineer, FSPL, India I'm a software engineer by profession, and I've done my engineering in computer science. It's been around 14 years I've been working in the field of website development and open-source technologies. Primarily, I work on PHP and MySQL-based projects and frameworks. Among them, I've worked on web frameworks like CodeIgnitor, Symfony, and Laravel. Apart from that, I've also had the chance to work on different CMS systems like Joomla, Drupal, and WordPress, and e-commerce systems like Magento, OpenCart, WooCommerce, and Drupal Commerce. I also like to attend community tech conferences, and as a part of that, I attended the 2016 Joomla World Conference held in Bangalore (India) and 2018 DrupalCon which was held in Mumbai (India). Apart from this, I like to travel, explore new places, and listen to music! |