A Program To Find The Length Of A String in PHP And MYSQL With Database
A Program To Find The Length Of A String in PHP And MYSQL With Database
In this tutorial, we will learn about the followings.
- Program To Find The Length Of A String in PHP
- Program To Find The Length Of A String in PHP with form
- Program To Find The Length Of A String in PHP And MYSQL With Database.
To find the length of a string, we need to use the built-in function strlen($parameteres). This functional automatically find the length of a string.
Program To Find The Length Of A String in PHP
In this example, the programmer stores the values in variables. However, if you interested to see the program about taking values in the form, please see example 2.
1 2 3 4 5 6 7 8 9 10 11 |
<html> <head> <title>Untitled Document</title> </head> <body> <?php $str = 'ali'; print strlen($str); ?> </body> </html> |
A Program To Find The Length Of A String in PHP by using forms
In this example, we will take the inputs from the user and store them in variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<html> <head> <title>Untitled Document</title> </head> <body> <form method="post"> <input type="text" name="name"/> <input type="submit" name="OK" /> </form> <?php if(isset($_POST['OK'])){ $str = $_POST['name']; print strlen($str); } ?> </body> </html> |
A Program To Find The Length Of A String in PHP And MYSQL by using forms
We are developing two web pages. First_page is the page in which all the main functionality of the program is done. Connection.php is the page containing only database connection for first_page.php.
first_page.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<html> <head> <title>Untitled Document</title> </head> <body> <form method="post"> <input type
="text" name="name"/> <input type="submit" name="OK" /> </form> <?php include ('connection.php'); if(isset($_POST['OK'])){ $str = $_POST['name']; $length=strlen($str); print strlen($str); $sql = "INSERT INTO length (first, second) VALUES ('$str', '$length')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); } ?> </body> </html> |
connection.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $servername="localhost"; $username="root"; $password=""; $database="string"; $conn= new mysqli($servername,$username,$password,$database); if(!$conn){ die("Failed".mysqli_connect_error()); } else echo "<br>Successfuly connected"; ?> |
Database and tables are shown below.