Verification: a143cc29221c9be0

Php check if no result

C++

#include

using namespace std;

int arraySortedOrNot(int arr[], int n)

{

    if (n == 1 || n == 0)

        return 1;

    if (arr[n - 1]

        return 0;

    return arraySortedOrNot(arr, n - 1);

}

int main()

{

    int arr[] = { 20, 23, 23, 45, 78, 88 };

    int n = sizeof(arr) / sizeof(arr[0]);

    if (arraySortedOrNot(arr, n))

        cout "Yes\n";

    else

        cout "No\n";

}

Java

class CkeckSorted {

    static int arraySortedOrNot(int arr[], int n)

    {

        if (n == 1 || n == 0)

            return 1;

        if (arr[n - 1] 2])

            return 0;

        return arraySortedOrNot(arr, n - 1);

    }

    public static void main(String[] args)

    {

        int arr[] = { 20, 23, 23, 45, 78, 88 };

        int n = arr.length;

        if (arraySortedOrNot(arr, n) != 0)

            System.out.println("Yes");

        else

            System.out.println("No");

    }

}

Python3

def arraySortedOrNot(arr):

    n = len(arr)

    if n == 1 or n == 0:

        return True

    return arr[0] = arr[1] and arraySortedOrNot(arr[1:])

arr = [20, 23, 23, 45, 78, 88]

if arraySortedOrNot(arr):

    print("Yes")

else:

    print("No")

C#

using System;

class CkeckSorted {

    static int arraySortedOrNot(int[] arr, int n)

    {

        if (n == 1 || n == 0)

            return 1;

        if (arr[n - 1]

            return 0;

        return arraySortedOrNot(arr, n - 1);

    }

    public static void Main(String[] args)

    {

        int[] arr = { 20, 23, 23, 45, 78, 88 };

        int n = arr.Length;

        if (arraySortedOrNot(arr, n) != 0)

            Console.WriteLine("Yes");

        else

            Console.WriteLine("No");

    }

}

Javascript

function arraySortedOrNot(arr, n)

{

    if (n == 1 || n == 0)

        return 1;

    if (arr[n - 1]

        return 0;

    return arraySortedOrNot(arr, n - 1);

}

let arr = [ 20, 23, 23, 45, 78, 88 ];

let n = arr.length;

if (arraySortedOrNot(arr, n) != 0)

    document.write("Yes");

else

    document.write("No");

C++

#include

using namespace std;

bool arraySortedOrNot(int a[], int n)

{

    if (n == 1 || n == 0)

    {

        return true;

    }

    return a[n - 1] >= a[n - 2] &&

     arraySortedOrNot(a, n - 1);

}

int main()

{

    int arr[] = { 20, 23, 23, 45, 78, 88 };

    int n = sizeof(arr) / sizeof(arr[0]);

    if (arraySortedOrNot(arr, n))

    {

        cout "Yes"

    }

    else

    {

        cout "No"

    }

    return 0;

}

Java

class GFG {

    static boolean arraySortedOrNot(int a[], int n)

    {

        if (n == 1 || n == 0)

            return true;

        return a[n - 1] >= a[n - 2]

            && arraySortedOrNot(a, n - 1);

    }

    public static void main(String[] args)

    {

        int arr[] = { 20, 23, 23, 45, 78, 88 };

        int n = arr.length;

        if (arraySortedOrNot(arr, n))

            System.out.print("Yes");

        else

            System.out.print("No");

    }

}

Python3

def arraySortedOrNot(arr, n):

    if (n == 0 or n == 1):

        return True

    return (arr[n - 1] >= arr[n - 2] and

            arraySortedOrNot(arr, n - 1))

arr = [ 20, 23, 23, 45, 78, 88 ]

n = len(arr)

if (arraySortedOrNot(arr, n)):

    print("Yes")

else:

    print("No")

C#

using System;

class GFG{

static bool arraySortedOrNot(int[] a, int n)

{

    if (n == 1 || n == 0)

    {

        return true;

    }

    return a[n - 1] >= a[n - 2] &&

     arraySortedOrNot(a, n - 1);

}

static public void Main()

{

    int[] arr = { 20, 23, 23, 45, 78, 88 };

    int n = arr.Length;

    if (arraySortedOrNot(arr, n))

    {

        Console.WriteLine("Yes");

    }

    else

    {

        Console.WriteLine("No");

    }

}

}

Javascript

function arraySortedOrNot(a, n)

{

    if (n == 1 || n == 0)

    {

        return true;

    }

    return a[n - 1] >= a[n - 2] &&

     arraySortedOrNot(a, n - 1);

}

let arr = [ 20, 23, 23, 45, 78, 88 ];

let n = arr.length;

if (arraySortedOrNot(arr, n))

{

    document.write("Yes" + "
"
);

}

else

{

    document.write("No" + "
"
);

}

C++

#include

using namespace std;

bool arraySortedOrNot(int arr[], int n)

{

    if (n == 0 || n == 1)

        return true;

    for (int i = 1; i

        if (arr[i - 1] > arr[i])

            return false;

    return true;

}

int main()

{

    int arr[] = { 20, 23, 23, 45, 78, 88 };

    int n = sizeof(arr) / sizeof(arr[0]);

    if (arraySortedOrNot(arr, n))

        cout "Yes\n";

    else

        cout "No\n";

}

Java

class GFG {

    static boolean arraySortedOrNot(int arr[], int n)

    {

        if (n == 0 || n == 1)

            return true;

        for (int i = 1; i

            if (arr[i - 1] > arr[i])

                return false;

        return true;

    }

    public static void main(String[] args)

    {

        int arr[] = { 20, 23, 23, 45, 78, 88 };

        int n = arr.length;

        if (arraySortedOrNot(arr, n))

            System.out.print("Yes\n");

        else

            System.out.print("No\n");

    }

}

Python3

def arraySortedOrNot(arr, n):

    if (n == 0 or n == 1):

        return True

    for i in range(1, n):

        if (arr[i-1] > arr[i]):

            return False

    return True

arr = [20, 23, 23, 45, 78, 88]

n = len(arr)

if (arraySortedOrNot(arr, n)):

    print("Yes")

else:

    print("No")

C#

using System;

class GFG

{

    static bool arraySortedOrNot(int []arr, int n)

    {

        if (n == 0 || n == 1)

            return true;

        for (int i = 1; i

            if (arr[i - 1] > arr[i])

                return false;

        return true;

    }

    public static void Main(String[] args)

    {

        int []arr = { 20, 23, 23, 45, 78, 88 };

        int n = arr.Length;

        if (arraySortedOrNot(arr, n))

            Console.Write("Yes\n");

        else

            Console.Write("No\n");

    }

}

What is Form?

When you login into a website or into your mail box, you are interacting with a form.

Forms are used to get input from the user and submit it to the web server for processing.

 The diagram below illustrates the form handling process.

A form is an HTML tag that contains graphical user interface items such as input box, check boxes radio buttons etc.

The form is defined using the

...
tags and GUI items are defined using form elements such as input.

In this tutorial, you will learn-

  • When and why we are using forms?
  • Create a form
  • POST method
  • GET method
  • GET vs POST Methods
  • Processing the registration form data
  • More examples

When and why we are using forms?

  • Forms come in handy when developing flexible and dynamic applications that accept user input.
  • Forms can be used to edit already existing data from the database

Create a form

We will use HTML tags to create a form. Below is the minimal list of things you need to create a form.

  • Opening and closing form tags
  • Form submission type POST or GET
  • Submission URL that will process the submitted data
  • Input fields such as input boxes, text areas, buttons,checkboxes etc.

The code below creates a simple registration form



	Registration Form

Registration Form

First name:
Last name:

Viewing the above code in a web browser displays the following form.

HERE,

  • are the opening and closing form tags
  • action="registration_form.php" method="POST"> specifies the destination URL and the submission type.
  • First/Last name: are labels for the input boxes
  • are input box tags

  • is the new line tag
  • is a hidden value that is used to check whether the form has been submitted or not
  • is the button that when clicked submits the form to the server for processing

Submitting the form data to the server

The action attribute of the form specifies the submission URL that processes the data. The method attribute specifies the submission type.

PHP POST method

  • This is the built in PHP super global array variable that is used to get values submitted via HTTP POST method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method is ideal when you do not want to display the form post values in the URL.
  • A good example of using post method is when submitting login details to the server.

It has the following syntax.

  HERE,

  • “$_POST[…]” is the PHP array
  • “'variable_name'” is the URL variable name.

PHP GET method

  • This is the built in PHP super global array variable that is used to get values submitted via HTTP GET method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method displays the form values in the URL.
  • It’s ideal for search engine forms as it allows the users to book mark the results.

It has the following syntax.

  HERE,

  • “$_GET[…]” is the PHP array
  • “'variable_name'” is the URL variable name.

GET vs POST Methods

POST GET
Values not visible in the URL Values visible in the URL
Has not limitation of the length of the values since they are submitted via the body of HTTP Has limitation on the length of the values usually 255 characters. This is because the values are displayed in the URL. Note the upper limit of the characters is dependent on the browser.
Has lower performance compared to Php_GET method due to time spent encapsulation the Php_POST values in the HTTP body Has high performance compared to POST method dues to the simple nature of appending the values in the URL.
Supports many different data types such as string, numeric, binary etc. Supports only string data types because the values are displayed in the URL
Results cannot be book marked Results can be book marked due to the visibility of the values in the URL

The below diagram shows the difference between get and post

 

Processing the registration form data

The registration form submits data to itself as specified in the action attribute of the form.

 When a form has been submitted, the values are populated in the $_POST super global array.

We will use the PHP isset function to check if the form values have been filled in the $_POST array and process the data.

We will modify the registration form to include the PHP code that processes the data. Below is the modified code



	Registration Form //this code is executed when the form is submitted

        

Thank You

You have been registered as

Go back to the form

Registration Form

First name:
Last name:

  HERE,

  • checks if the form_submitted hidden field has been filled in the $_POST[] array and display a thank you and first name message.

    If the form_fobmitted field hasn’t been filled in the $_POST[] array, the form is displayed.

More examples

Simple search engine

We will design a simple search engine that uses the PHP_GET method as the form submission type.

For simplicity’s sake, we will use a PHP If statement to determine the output.

We will use the same HTML code for the registration form above and make minimal modifications to it.



	Simple Search Engine

Search Results For

The GET method displays its values in the URL

Sorry, no matches found for your search term

Go back to the form

Simple Search Engine - Type in GET

Search Term:

View the above page in a web browser

The following form will be shown

Type GET in upper case letter then click on submit button.

The following will be shown

The diagram below shows the URL for the above results

Note the URL has displayed the value of search_term and form_submitted. Try to enter anything different from GET then click on submit button and see what results you will get.

Working with check boxes, radio buttons

If the user does not select a check box or radio button, no value is submitted, if the user selects a check box or radio button, the value one (1) or true is submitted.

We will modify the registration form code and include a check button that allows the user to agree to the terms of service.



	Registration Form

You have not accepted our terms of service

Thank You

You have been registered as

Go back to the form

Registration Form

First name:
Last name:
Agree to Terms of Service:

View the above form in a browser

Fill in the first and last names

Note the Agree to Terms of Service checkbox has not been selected.

Click on submit button

You will get the following results

Click on back to the form link and then select the checkbox

Click on submit button

You will get the following results

What is Regular expression in PHP?

PHP Regular Expression also known as regex are powerful pattern matching algorithm that can be performed in a single expression. Regular expressions use arithmetic operators such as (+,-,^) to create complex expressions. They can help you accomplish tasks such as validating email addresses, IP address etc.

Built-in Regular expression Functions in PHP

PHP has built in functions that allow us to work with regular functions which we will learn in this PHP Regular Expressions tutorial. Let’s look at the commonly used regular expression functions in PHP.

  • preg_match() in PHP – this function is used to perform pattern matching in PHP on a string. It returns true if a match is found and false if a match is not found.
  • preg_split() in PHP – this function is used to perform a pattern match on a string and then split the results into a numeric array
  • preg_replace() in PHP – this function is used to perform a pattern match on a string and then replace the match with the specified text.

Below is the syntax for a regular expression function such as PHP preg_match(), PHP preg_split() or PHP preg_replace().

HERE,

  • "function_name(...)" is either PHP preg_match(), PHP preg_split() or PHP preg_replace().
  • "/.../" The forward slashes denote the beginning and end of our PHP regex tester function
  • "'/pattern/'" is the pattern that we need to matched
  • "subject" is the text string to be matched against

Let’s now look at practical examples that implement the above PHP regex functions.

PHP Preg_match()

The first example uses the preg_match() in PHP function to perform a simple pattern match for the word guru in a given URL.

The code below shows the implementation for preg_match() tester function for the above example.

  Browse to the URL http://localhost/phptuts/preg_match_simple.php

Let’s examine the part of the code responsible for our output "preg_match('/guru/', $my_url)"   HERE,

  • "preg_match(...)" is the PHP regex function
  • "'/guru/'" is the regular expression pattern to be matched
  • "$my_url" is the variable containing the text to be matched against.

The diagram below summarizes the above points  

PHP Preg_split()

Let’s now look at another example that uses the preg_split() in PHP function.

We will take a string phrase and explode it into an array; the pattern to be matched is a single space.

The text string to be used in this example is "I Love Regular Expressions".

The code below illustrates the implementation of the above example.

  Browse to the URL http://localhost/phptuts/preg_split.php

PHP Preg_replace()

Let’s now look at the preg_replace() in PHP function that performs a pattern match and then replaces the pattern with something else.

The code below searches for the word guru in a string.

It replaces the word guru with the word guru surrounded by css code that highlights the background colour.

Guru', $text);

echo $text;

?>

  Assuming you have saved the file preg_replace.php, browser to the URL http://localhost/phptuts/preg_replace.php

Regular Expression Metacharacters

The above examples used very basic patterns; metacharacters simply allow us to perform more complex pattern matches such as test the validity of an email address. Let’s now look at the commonly used metacharacters.

Metacharacter Description Example
. Matches any single character except a new line /./ matches anything that has a single character
^ Matches the beginning of or string / excludes characters /^PH/ matches any string that starts with PH
$ Matches pattern at the end of the string /com$/ matches guru99.com,yahoo.com Etc.
* Matches any zero (0) or more characters /com*/ matches computer, communication etc.
+ Requires preceding character(s) appear at least once /yah+oo/ matches yahoo
\ Used to escape meta characters /yahoo+\.com/ treats the dot as a literal value
[...] Character class /[abc]/ matches abc
a-z Matches lower case letters /a-z/ matches cool, happy etc.
A-Z Matches upper case letters /A-Z/ matches WHAT, HOW, WHY etc.
0-9 Matches any number between 0 and 9 /0-4/ matches 0,1,2,3,4

  The above list only gives the most commonly used metacharacters in regular expressions.

Let’s now look at a fairly complex example that checks the validity of an email address.

This email address is being protected from spambots. You need JavaScript enabled to view it.";
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $my_email)) {
echo "$my_email is a valid email address";
}
else
{
  echo "$my_email is NOT a valid email address";
}
?>

Explaining the pattern "[/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/]"

HERE,

  • "'/.../'" starts and ends the regular expression
  • "^[a-zA-Z0-9._-]" matches any lower or upper case letters, numbers between 0 and 9 and dots, underscores or dashes.
  • "+@[a-zA-Z0-9-]" matches the @ symbol followed by lower or upper case letters, numbers between 0 and 9 or dashes.
  • "+\.[a-zA-Z.]{2,5}$/" escapes the dot using the backslash then matches any lower or upper case letters with a character length between 2 and 5 at the end of the string.

Browse to the URL http://localhost/phptuts/preg_match.php

As you can see from the above example breakdown, metacharacters are very powerful when it comes to matching patterns.

1. Main Web Page

Create and index.html file on your website using following content. This is the main HTML form that will display on the website page. You can simply integrate a form into any website page.

Submit Form without Page Refresh - PHP/jQuery - TecAdmin.net
Name:
Email:
Phone No:
Gender: Male Female

Your data will display below.....
==============================

2. Create JavaScript

Now create a JavaScript page with name submit.js inside js directory using following content. You can optimize this JavaScript by adding the jQuery validations.

function SubmitFormData(){
    var name = $("#name").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var gender = $("input[type=radio]:checked").val();
    $.post("submit.php", { name: name, email: email, phone: phone, gender: gender },
    function(data) {
	 $('#results').html(data);
	 $('#myForm')[0].reset();
    });
}

3. Create PHP Script

Now create a PHP script named submit.php. This script will do all your backed operations. When we submit the web form, This script will get all arguments from the POST. You can customize this script according to your requirement like save values in the database, Email records to Admin, etc.

";
  echo $_POST['email'] ."
"; echo $_POST['phone'] ."
"; echo $_POST['gender'] ."
"; echo "==============================
"; echo "All Data Submitted Successfully!"; ?>