Trending February 2024 # How When Statement Works In Kotlin With Examples? # Suggested March 2024 # Top 8 Popular

You are reading the article How When Statement Works In Kotlin With Examples? updated in February 2024 on the website Bellydancehcm.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested March 2024 How When Statement Works In Kotlin With Examples?

Introduction to Kotlin when

Web development, programming languages, Software testing & others

Syntax

In kotlin language, it has many default keywords, variables, and functions to implement the application. One of the default keywords can be used in both expression and non-expression scenarios.

val variablename; when(variablename) { —some coding logics it depends upon the requirement— } }

The above codes are the basic syntax for utilizing the when keyword on the kotlin codes. We want to write the expression based on the function’s requirement on the child function, or the inheritance concept will be implemented on the application.

Working of when statement in Kotlin

Generally, “when” is one of the keywords, and it is used in both expression and non-expression; it also checks and combined with the valid conditional statements depending on the calculation based on the user requirements, in kotlin, when it is constructed, and it can be thought of as the replacement for the switch case statement, which is similar to the other languages when the keyword is used as the expression and validating the condition like matching the values for overall expression. If condition statement, the values of the individual branches are always ignored, each branch can be of the block, and its value is the last expression of the block values.

Else loop is evaluated if none of the other branches and the condition is satisfied with the boolean condition statements; else, a loop is mandatory unless the compiler can prove that all possible cases are covered with the loop conditions. The scope of the variable is introduced with the keyword for when the subject is restricted to the body of this keyword for when expression. We can check and validate the condition using a particular type in both compile and runtime in the kotlin operators. With the help of conditional statements, the loop will use the control, and it executes the other conditions based on their needs.

Examples of Kotlin when

Below are the examples of Kotlin when:

Example #1

Code:

package one; println("Welcome To My Domain it’s a first example regarding the When keyword using the kotlin program logics") var first = 7 var second = when(first) { } println("$second") var third = when(second) { } println("Thank you users have a nice day please find your outputs $first") }

Output:

We used the above example when keywords in basic formats like int, string, string, and string formats. We can print the statements on the user console screen.

Example #2

Code:

package one; println("Welcome To My Domain it’s a second example regarding the When keyword using the kotlin program logics") var first = 'A' when(first){ } println("Thank you users have a nice day kindly try again please keep and stay with our application $first") var yrsage = 16 when(yrsage) { val numlast = 100 - yrsage println("Your marraige age is in $numlast years") } } }

Output:

Example #3

Code:

package one; enum class Third(val exampl: Boolean = false){ January(true), February, March, April(true), May, June, July(true), August, September, October, November, December(true); companion object{ fun demo(obj: Third): Boolean { } } } fun demos(th: Third) { when(th) { } } fun main(){ println("Welcome To My Domain its a third example regarding the When keyword using the kotlin program logics") for(eg in Third.values()) { println("${eg.ordinal} = ${eg.name} and is months in ${eg.exampl}") } val demo1 = Third.April; println("Thank you users your current month is ${Third.demo(demo1)}") }

Output:

In the final example, we used to calculate the current month status by using the boolean condition. We used the when keyword to print the enum class value on the function.

Conclusion

In the conclusion part, kotlin when is one of the conditional statements like if, else, etc. The when keyword supports non -conditional expressions also instead of the switch case statement, these statements will execute the user inputs on each step. It supports all types of user browsers, so it’s compatible when keyword satisfied with some range intervals, which depends upon the requirement.

Recommended Articles

This is a guide to Kotlin when. Here we discuss the introduction, syntax, and working of when statement in Kotlin along with the examples and outputs. You may also have a look at the following articles to learn more –

You're reading How When Statement Works In Kotlin With Examples?

How Does Ansible Firewalld Works With Examples

Introduction to Ansible Firewalld

In Ansible, we have many modules that provide us the ability to perform operational work on remote hosts. Especially, operations which are to be done on Linux remote hosts. One such module is named firewalld, which is used to manage firewall rules of Linux systems. In this topic, we are going to learn about Ansible Firewalld.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As we know that Linux systems can have a firewalld daemon that is used to allow/block access to/from services, networks, and ports by updating running or permanent firewall rules on the machine via firewall-cmd utility. The same is managed by Ansible using the firewalld module.

Explaining the Ansible Firewalld

Ansible firewalld is the module that is used to update firewall rules on remote hosts. The remote hosts are the Linux machines here. Ports can be TCP or UDP, which can be enabled or disabled. Similarly, services can be allowed or blocked.

We should note below points while working with Ansible firewalld module: –

As per the latest Ansible firewalld module, the firewalld version on the hosts, where firewall rules will be modified, should have equal to or greater than 2.11.

This module is not tested in Debian based

Requires python2 bindings of firewalld. Where python2 bindings are not available, python3 bindings can be used but we must set ansible_python_interpreter to python3 interpreter path and install python3

There is a known limitation in Ansible firewalld due to which zone transactions must explicitly be permanent. This also means that when we add a zone and want to perform immediate actions on it, we need to reload the firewalld service. But take care while doing that because reloading firewalld will undo all non-permanent actions performed

This module is not guaranteed to have backward

Along with the above points, we should also know below terminologies which are heavily used in firewalld.

Zone: Zone is a logical network location which can be arbitrary but can be defined in terms of the network from which traffic will originate, or a location to which your local network interface is connected.

Services: Services are the series of ports and protocol combination which works as the socket, that our host is listening on, which then can be placed in one or more

Ports: These are the logical constructs which are representing a service endpoint

How Does Ansible Firewalld Works?

Because handling rules in an ad hoc way will end up in a mess and we need to spend hours and network support to identify the problematic parts in our firewall rules.

icmp_block: The icmp block we like to remove or add from or to a zone in firewall rules

immediate: if the permanent parameter is used, should this be applied

interface: The interface we like to remove or add to or from a zone in firewall rules

permanent: Should the configuration be in permanent rule, which persists across reboots or in running configuration temporarily. When this is “no”, then by default immediate is “yes”.

Acceptable values are either “yes” or “no”.

1. port: Name or port or port range to remove or add to or from firewalld. When giving ranges, it must be in the form of PORT/PROTOCOL or PORT-PORT/PROTOCOL for port

2. rich_rule: rich rule to add or remove to or from

3. service: The service which needs to be added or removed to or from firewalld. The service must be listed in the output of the “firewall-cmd –get-services” command on remote

4. source: The source network you would like to be removed or added to or from in firewalld rules.

5. state: Enable or disable a setting. Below are acceptable values from which present and absent

are used in case of zone level operation.

absent

present

enabled

disabled

6. timeout: The time for which rule should be in effect when set as non-permanent

7. zone: The firewall zone to be added or removed. The public is default zone from upstream but this can be configured. Some out of box defaults are block, DMZ, external, internal, trusted, work. This list can be extended based on a per system

Example of Ansible Firewalld

Now by using examples, we will try to learn about Ansible firewalld, which you might have to use in day to day operations. We will take some examples, but before going there, we first understand our lab, we used for testing purpose.

Here we have an Ansible control server named ansible-controller and one remote host named host- remote. We will create playbooks and run Ansible commands on the ansible-controller node and see the results on the remote host.

Also, on the remote host, below is the current status of firewalld.

firewall-cmd --state

firewall-cmd --get-services

firewall-cmd --get-zone

firewall-cmd --list-all

We will use a playbook with below content: –

When we execute it like below we get below output: –

ansible-playbook ansible_firewalld_enable_service.yaml

firewall-cmd --list-all

In this example, we will see how to enable a port in remote host, for this we have a playbook like below content: –

port: 443/tcp state: enabled

ansible-playbook ansible_firewalld_disable_port.yaml

Now checking on the remote host, we will see this port is listed in the output of –list-all now like below, but it was not there previously: –

firewall-cmd --list-all

In this example, we will enable an ip range for a zone, for this we have a playbook like below, Note here that as we are doing a zone related transaction so for this to work we need to make it permanent and also immediately reload firewalld on the remote host, like below: –

name: we have to reload firewalld else zone transactions will not be realised command: firewall-cmd –reload

After executing this playbook we get the below output: –

ansible-playbook ansible_firewalld_enable_source_network.yaml

On the remote host, we can see that the mentioned network is listed on the concerned zone’s allowed list

firewall-cmd --zone=internal --list-all

Conclusion

As we have seen that Ansible firewalld is a very powerful module which can be very useful if you have supported network and your remote hosts are supportable in all ways. But points to note that it is not an easy task to have a track of all the firewall rule, especially when we have permanent and non- permanent rules. So that preparation is needed before-hand.

Recommended Articles

This is a guide to Ansible Firewalld. Here we discuss the introduction to Ansible Firewalld along with the working, detailed explanation and respective examples. You may also look at the following articles to learn more –

How Linux Zcat Command Works With Programming Examples

Introduction to Linux Zcat

In Linux Zcat, Linux ecosystem, we are able to compress the files with the help of gzip or gunzip. But when we need to check the compress file content without uncompressing the compressed files then we are using the zcat command.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Note: The zcat utility is only working with the gzip or gunzip compress files. If it will work with the tar compression technique, it will not work. It will provide the garbage value. The Zcat command utility was written by Miloslav Trmac.

Syntax

Syntax of Zcat command:

zcat [ option ] [ name ]

zcat: We can use the zcat keyword in the syntax or command. It will take the two-argument as an option and file name. Provide the end result to display the compress file content without uncompressing the file.

option: We can provide the different flags as options that are compatible with the zcat command example “-fhLV”.

How does Linux Zcat Command work?

Basically, the zcat is a command-line utility. It is used to display the compressed file content without uncompressing the compressed file. It will swell the compressed file into standard output and allowing to end-user to look at the contents available in it. A few more updates, the zcat is compatible to run the gunzip –c command.

The zcat command is majorly working with two components i.e. option and the compressed file name. When any end-user or client will trigger the zcat command? The zcat command will accept the input as an option and compressed file as standard input and display the compress file content as standard output.

Examples to Implement Linux Zcat Command

Below are examples mentioned:

1. Zcat Command

It is a very simple and common way to use the zcat command in a Linux environment. It will print the compress file content which is provided to the zcat command.

Code:

zcat data.txt.gz

Output:

Explanation: We are creating a few sample files in the data directory. As per screenshot 1 (a), we have created the chúng tôi file and compress it with the help of gzip. With the help of the zcat command, we are able to read the compressed file content (refer screenshot 1 (b)). In the same file contented, we are not able to read with normal cat command (refer screenshot 1 (c)).

2. Zcat Command: Multiple Compressed File View

In zcat command, we are having the functionality to view the multiple compressed files at the same time in the same command.

Code:

zcat chúng tôi data1.txt.gz

Output:

Explanation: As per the above zcat command, we are able to read the number of compress file at the same time. From the data directory, we are reading the two compress files i.e. chúng tôi data1.txt.gz at the same time.

3. Zcat Command: Force File View

In the zcat command, we are having the functionality to display the file content whether the file is compressed or not. To print the file contented forcefully, we need to use the “-f” option with the zcat command.

zcat -f data.txt.gz

Output:

Explanation: As per the below screenshot, the zcat file is compatible with gzip files. But if we need to read the content of the normal file, it will not possible. Hence we need to use the “-f” option to read the file forcefully with the zcat command.

4. Zcat Command: With More Command

When do we need to display the huge compress file data? It would be more difficult to display the data in a single window. We can use the more command to view the zcat data.

Note: We can use the more command functionality with the zcat command.

Code:

Output:

Explanation: If we need to view the huge amount of log data, it would be more difficult to view or print in a single screen. We can use the more command functionality to read the huge amount of compressed log files.

Note: we are using pipe functionality to read the zcat data in more command.

5. Zcat Command: With Less Command

In the zcat command, we are having the functionality to display the huge amount of data with less command utility.

Note: We can use the more command functionality with the zcat command.

Code:

Output:

Explanation: As per the more command, we can read the huge compressed file. Similarly, we can use the less command functionality to read the huge amount of compressed log files.

Note: we are using pipe functionality to read the zcat data in less command.

6. Zcat command: Print Compression Information

With the help of the zcat command, we are able to get the compression information of the file. To print the compression information, we need to use the “-l” option with the zcat command.

Code:

zcat -l log.txt.gz

Output:

Explanation: As per the above command, we are able to find the compression status of the compressed file. We are using one of the log files i.e. chúng tôi The compression information will display the compressed, uncompressed, ratio information with the compressed file name.

7. Zcat Command – suppress Warning

Some times while printing any compress data, the lots of warning come. To avoid this condition, we need to use the “-q” option with the zcat command.

Code:

zcat -q data.txt.gz

Output:

Explanation: When we are reading the compress file content, sometimes the number of warning comes due to different issues. As per the below screenshot, we can avoid the warring while reading the compressed file contented. We can use the “-q” option with the zcat command to avoid the warning.

Conclusion

We have seen the uncut concept of “Linux Zcat Command” with the proper example, explanation and command with different outputs. The zcat command is compatible with the gunzip –c command. It will really helpful to print the compress file content without uncompressing the actual file.

Recommended Articles

We hope that this EDUCBA information on “Linux Zcat” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

Learn The Basic Concept Of Fopen Statement (Examples)

Introduction to Matlab fopen

Matlab fopen statement is used for an open a file or obtains information about open files. The file handling operations like reading from file or writing on a file for these operations we need to first open a file for that fopen statement is used. We open a file in binary mode or text mode and we specify the access type like r, w, r+, a, etc. and the default access type is read. The fopen statement returns data stored in fileID1, fileID1 is a file identifier on open file.

Syntax: 

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

fileID1 = fopen(filename)

fileID1 = fopen(filename,permission)

fileID1 = fopen(filename,permission,machinefmt,encodingIn)

fIDs = fopen('all')

filename = fopen(fileID)

[filename,permission,machinefmt,encodingOut] = fopen(fileID)

How to Use Matlab fopen?

We use fopen statement for only open the file for these we don’t need to specify anything simply we take a fopen statement and that file name which we want to open. But in another case, if we want to write or read from file we must specify the access type in fopen statement. For these we want to specify the permission, the permission is as followed:

‘r’: The file is open for reading. And it’s a default condition.

‘r+’: The file is open for both reading as well as writing.

‘w’: It will delete the contents of an existing file or simply it creates a new file and opens it for writing.

‘w+’: It will delete the contents of an existing file or simply it creates a new file, and open it for both readings as well as writing.

‘a’: It will create and open a new file or open an existing file for writing, appending to the end of the file.

‘a+’: It will create and open a new file or open an existing file for reading and writing, appending to the end of the file.

‘A’: It will append without automatic flushing; used with tape drives

Examples to Implement Matlab fopen

Below are the examples of Matlab fopen:

Example #1

Let us see one example, in this example, we open the text file using fopen and read the data from that file. For these first we take a fopen statement, in this fopen we specify that which file we want to open that file name and the access type mode of that file, for those we take fopen in parenthesis text file name with .txt extension chúng tôi and the access type mode is read ‘r’ and these two arguments are separated by a comma. And this fopen statement we take in a file identifier that is fileID1. And then we simply read that file using a fscanf statement. And lastly, we need to close that file using a file identifier and close statement; the file close statement is fclose and files identifier of open file that is fileID1.

fileID1 = fopen(‘textfile1.txt’,’r’);formatSpec = ‘%s’;A1 = fscanf(fileID1,formatSpec)fclose(fileID1);

Output:

Example #2

Let us see an example related to the fopen statement, we already created one text file with extension .txt ‘file1.txt’ in that text file we write something information. For reading or displaying one line from that text file, we write a Matlab code, in Matlab code, we use a fopen statement fopen statement is used for an open a file or obtain information about an open file. In this example, we used fopen statement for an open a text file which we earlier created with the name ‘file1’. Then we assign a file identifier to that fopen statement. Then we use an ‘fgetl’ function it is an inbuilt function available on Matlab it used for reading one line from a file. This readied line stored in line variable and we displaying that data and then we close the file, for closing a file we take fclose in that file identifier.

Code:

tline = fgetl(fileID)fclose(fileID);

Example #3

Let us see one example related to fopen statement, in this example, we create a matrix using a magic function, the magic function is nothing but it creates a matrix of n-n number in our example it creates 4- by-4 matrix. The matrix is stored in variable A1. This matrix we want to write in a file for that we need to open that file. To open a file we use a fopen statement, fopen in parenthesis the file name in which we want to write a matrix and a type of access mode that is written ‘w’ and these we assign to the fileID1, fileID1 is a file identifier of an open file. Then we use a fprintf statement to write that matrix on a text file. Then we simply close that open file by using a file close statement and a file identifier of an open file. And for verification that the matrix is written on that text file or not we use a type function, type in parenthesis that file name in which we write a matrix.

Code:

A1 = magic(4);fileID1 = fopen (‘myfile1.txt’,’w’);nbytes1 = fprintf(fileID1,’%6d %6d %6d %6dn’,A1)fclose(fileID1);type(‘myfile1.txt’)

Output:

Conclusion

In this article we saw the basic concept of fopen statement, basically fopen is nothing but an open a file or obtain information about open files. We also saw the different syntax for fopen statement used in Matlab code. Also, we saw the different some examples related to fopen.

Recommended Articles

This is a guide to Matlab fopen. Here we discuss the Introduction to Matlab fopen and its Syntax along with different Examples and its Code Implementation. You can also go through our suggested articles to learn more –

How Keyfile Works In Mongodb?

Definition of MongoDB keyfile

MongoDB keyfile is used to authenticate our database from unauthorized access, we can authenticate our replica set using MongoDB keyfile. To enforce the access of keyfile using replica set we require to configure the security between each replica set using user access control. After implementing keyfile authentication each member from the replica set will use the same authentication mechanism which was we have developed using MongoDB keyfile. We can generate our keyfile using any method which was available, also we can generate our keyfile between 6 to 1024 characters.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Below is the syntax of keyfile in MongoDB.

1) Create keyfile –

2) Set the path of keyfile in MongoDB configuration file –

3) Start the MongoDB instance using keyfile –

Parameter description syntax of keyfile in MongoDB is as follows.

1) Openssl – This is a Linux command which was used to generate keyfile. Using OpenSSL we can generate the 1024 pseudo random password.

2) Encoding method – This parameter is defined as the method which was we have using to encode the pseudo-random code.

3) No of keyfile characters – This is defined as no of characters which was we have used while generating a keyfile.

4) Path and name of keyfile – This parameter is defined as the path where we have stored our keyfile. Also, we need to give keyfile name at the time of keyfile creation.

5) Security – This is the parameter of the MongoDB configuration file which was used to define the authentication method of the MongoDB server. Using this parameter we can define authentication as keyfile in MongoDB.

How Keyfile works in MongoDB?

To use keyfile authentication we need to create database admin users for database administration purpose only.

The first step is to create keyfile, the main purpose of the MongoDB keyfile is to authenticate every database instance using the contents of the keyfile. Using this content and proper keyfile only the MongoDB instance is connected to the replica set.

If suppose we don’t have keyfile we cannot connect the replica set, it will show the error like authentication is failed.

We can generate a minimum of 6 characters and a maximum of 1024 characters keyfile in MongoDB. Also, keyfile contains characters in the base-64 format.

After generating the keyfile we need to copy the file on every server on which the replica set is running. We need to ensure that the user which was running the mongod instance is the owner of the file and he is able to access that keyfile.

After copying keyfile on each server we need to change the security method to keyfile and need to restart all the replica set.

rs.initiate ()

rs.status ()

After connecting to the primary replica set we need to create database user which have admin privileges on the database server.

After connecting to the primary replica set we need to check the user authentication using the replica set.

After checking user authentication we need to create a cluster-admin user for the admin database.

Example

The below steps shows create keyfile in MongoDB. We have to create keyfile of the replica set.

1) Create keyfile

In the below example, we have to create a keyfile name as mongodb_keyfile and we have to store this file into the MongoDB data directory.

We have created the keyfile using the OpenSSL command. We have used 525 characters to create keyfile.

Code:

cat /var/lib/mongo/mongodb_keyfile

Figure – Example to create keyfile.

2) Copy the keyfile on every replica member

After creating the keyfile we need to copy this file on each replica set. We have using cp command to copy the file.

Code:

ls -lrt /var/lib/mongo2/mongodb_keyfile

Figure – Example to Copy the keyfile on every replica member.

3) Enable the access control on every replica set

We have enabled access control on every server. For enabling the access control we need to configure the same into our configuration file.

After enabling the authentication we need to restart the replica set.

Code:

cat /etc/mongod.conf

Figure – Example of enable the access control on replica set.

systemctl status mongod

Figure – Example start the MongoDB replica instance.

4) Connect to the replica set

After starting the replica set connect to the replica instance using mongo command.

Code:

mongo

Figure – Example to connect the replica set.

5) After connecting to the replica set check the status of replication

After connecting to the database server need to check the status of the replica set. We have to check the replication status using rs. status () command.

Code:

rs.status ()

Figure – Example to check status of replica set.

6) Create the administrator user

Code:

roles: [ { role: “userAdminAnyDatabase” (Admin access of any database) , db: “admin” (database name) } ] )

Figure – Example to create administrator user.

7) Authenticate the user

In the below example, we have to authenticate our admin database user.

Code:

db.getSiblingDB ("admin" (database name) ).auth("mongodb_admin" (user name), passwordPrompt())

Figure – Example of authenticate the user.

8) Create cluster-admin user

In the below example, we have created the cluster-admin user for keyfile authentication.

Code:

roles: [ { role: “userAdminAnyDatabase” (Admin access of any database) , db: “admin” (database name) } ] } )

Figure – Example to create cluster-admin user.

Conclusion

MongoDB keyfile is used to authenticate the database from unauthenticated access. We need to create keyfile using the encryption method. In the above example, we have created keyfile using the OpenSSL command. Keyfile is very important and useful in MongoDB to authenticate the database from unauthorized access.

Recommended Articles

This is a guide to MongoDB keyfile. Here we discuss the definition, syntax, How keyfile works in MongoDB? examples with code implementation. You may also have a look at the following articles to learn more –

If Else Statement In Php

Introduction to if else Statement in PHP

The following article provides an outline on if else Statement in PHP. If else are the basic conditional statements that are normally used to perform different types of actions based on some of the different conditions. When we write any program/code, one has to take decisions(conditions) to perform certain types of tasks. So we need conditional statements(if, else, etc..,) to do our tasks using the code. “If” condition/statement will make the code run only if the condition is true whereas “else” is the condition which will run only if the “if” condition becomes false.

Start Your Free Software Development Course

Syntax:

if(condition statement){ Programming code statements to run only if the IF condition is TRUE } if(condition statement){ Another code to be executed with an extra IF condition. (Multiple iF statements can be ignored to if we don’t want) } else{ Code Here will run only if the IF condition becomes FALSE }

Flow Diagram

How if else Statement works in PHP?

if: If function works when the conditions inside of the function are true else compiler will go to else condition if extra if the condition is not there at first.

else: Else function works when the if, Extra if conditions fail in the program/code in PHP/any other Programming Language mostly.

Examples of if else Statement in PHP

Given below are the examples of if else Statement in PHP:

Example #1

This is the basic PHP program to know which number is greater which are stored in the number_one, number_two variables using one if and else conditions by comparing the two variable’s values.

Code:

<?php $number_one = 17; $number_two = 51; echo "$number_one is greater than $number_two"; }else{ echo "$number_two is greater than $number_one"; }

Output:

Example #2

This is the basic PHP code which is listed below to know whether Today/Present-day is Saturday/Sunday. Date(“d”) will have the value of Today/present day. It will be stored in s variable. By using the conditions of the “s” variable’s value the code will provide the output. If Today is Sat, code will print as “This is chúng tôi a nice weekend Bro!!” like that and If Today is Sun, the code will print as “Have a Happy Weekend! Now We are in Sunday!! <3” or else it will print “This is not Weekend. Go to the work Now..”

Code:

<?php $s = date("D"); if($s == "Sat"){ echo "This is chúng tôi a nice weekend Bro!!"; } if($s == "Sun"){ echo "Have a Happy Weekend! Now We are in Sunday!! <3 "; } else{ echo "This is not Weekend. Go to the work Now.."; }

Example #3

This is a PHP program to print “The Person is now Child” If age is either <18 using IF condition statement or “The Person is now Adult” will be printed.

Code:

<?php $age1 = 22; if($age1 < 18){ echo 'The Person is now Child'; } else{ echo 'The Person is now Adult'; }

Output:

Example #4

This is the PHP program to print addition, subtraction( by substracting from the big digit to the small digit by comparing number_1, number_2 variable’s value), multiplication, division(dividing the big number with the small number – This is got by comparing number_2, number_1 variable’s value) of the specified two numbers and also checking whether the number_1, number_2 variable’s value is a prime number or not and then also printing Square values of the number_1, number_2 variable’s values. You can change those numbers however you want or else you can have inputs from the users/customers using the input validation form using the HTML if you want.

<?php $number_1 = 22; $number_2 = 46; echo "First Number(number_1) = $number_1"; echo "Second Number(number_2) = $number_2"; echo "Addition of numbers: "; echo $number_1 + $number_2; echo "Substraction of numbers: "; echo $number_1 - $number_2 ;} else{ echo $number_2 - $number_1; } echo "Multiplication of Numbers : "; echo $number_1*$number_2; echo "Division of numbers:: "; echo $number_1/$number_2 ;} else{ echo $number_2/$number_1; } if($number_1%2==0){ if($number_1%3==0){ if($number_1%5==0){ if($number_1%7==0){ echo "1st Number : $number_1 is not prime number"; } echo "1st Number : $number_1 is not prime number"; } echo "1st Number : $number_1 is not prime number"; } echo "1st Number : $number_1 is not prime number"; } else{ echo "1st Number : $number_1 is a prime number"; } if($number_2%2==0){ if($number_2%3==0){ if($number_2%5==0){ if($number_2%7==0){ echo "2nd Number : $number_2 is not prime number"; } echo "2nd Number : $number_2 is not prime number"; } echo "2nd Number : $number_2 is not prime number"; } echo "2nd Number : $number_2 is not prime number"; } else{ echo "2nd Number : $number_2 is a prime number"; } echo "Square of number_1($number_1) = "; echo pow($number_1,2); echo "Square of number_2($number_2) = "; echo pow($number_2,2);

Output:

Example #5

This is the PHP Programming code to check whether on which mode a person is traveling using 3 Condition statements with different speed values to the speed1 variable. If the variable has numerical value which is less than 60 then “You are now in a safe driving mode” will be printed or if the speed1 variable is between 60 and 100 then “You are in the mode of burning extra fuel for your vehicle” will be printed or else “You are in the dangerous mode: Accidents may occur please be careful” will be printed. This is the sample example program of PHP to illustrate speed mode using speed1 variables value. One can automate this simple program by connecting the GPS values and check the variation in the mode in PHP/any other programming language too.

Code:

<?php $speed1 = 110; if($speed1 < 60) { echo "You are now in a safe driving mode"; } { echo "You are in the mode of burning extra fuel for your vehicle"; } else { echo "You are in the dangerous mode : Accidents may occur please be careful"; }

Output:

Recommended Articles

This has been a guide to if else Statement in PHP. Here we discuss the introduction, syntax, flowchart, and working of if else statements in PHP along with examples. You may also have a look at the following articles to learn more –

Update the detailed information about How When Statement Works In Kotlin With Examples? on the Bellydancehcm.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!