Posts

Showing posts from July, 2021

How to read write text-files in PHP

Image
  How to read / write /copy file content in PHP     PHP provided several predefined functions to read or write data from files. It is very common for developers to copy data from one source to another. File functions in PHP make things easier. In this article, we will use different types of file function to read data from files. Table of Contents Reading text Files Writing text Files Copy text Files To read a text from a file, we use the following methods in PHP fopen() feof() fread() fgets() fclose() fopen :   function returns a file handle(file pointer) fread :    reads specified no. of bytes fgets :     reads line of text every time. feof :      checks for end of file. Returns boolean value. fclose :   closes an opened file. File must be closed when it is opened.   menu Reading text Files   fopen Example :     index.php   <?php   $file_handle = fopen ( " about.txt" , "r" );   print $file_handle ;   fclose ( $file_handle );   ?>   o/p: Resource id #2

Submitting formated JSON with PHP form

Image
 Submitting formatted JSON Data with PHP In this blog, I am explaining how to store submitted form data in JSON format. So first I create a form that will allow users to add entries to the site. Entries are stored in a JSON file.  Form having some fields such as postDate title description.   When the form is submitted, PHP code checks if $_POST is empty or not. If not empty  it creates an array called $postArray with submitted form data Then it passes the array to json_encode() function. Json_encode built-in function converts PHP array  to JSON object and returns it. Then I concatenated ',' as a separator with other $json objects. Next file_put_contents function takes 3 parameters         1. file_name  // to write json data         2. json object         3. flag variable called: FILE_APPEND // appends entry at the end of the json file. Top↑ form_2_json.php <?php $ans = "false" ; // check if a form was submitted if ( ! empty ( $_POST ) ){ // convert form da

Basic String functions in PHP

Image
How to use Basic String function in PHP       Similar to array built-in functions, there are some string built-in functions available in PHP to deal with strings. There are plenty of string functions in PHP which help in performing several manipulations on string data. In this article I will give you an idea on some basic string functions which are most frequently used in PHP scripts.  Here is the list... strlen() strtoupper() strtolower()   ucfirst()   substring()   strcmp()   1. Strlen Function String Length: This function gives you the length of the given string. Generally this function is used in validating user inputs, where the user is supposed to give specified length of characters. Syntax : Strlen(string); Example : <?php   $text = " e-Trainings"; print "Text length is =". strlen($text)."<br>"; ?>   Menu   2. Strtoupper Function strtoupper( $string ): is a string conversion function. Convert given  string into uppercase   Syntax  s

Most Useful Array Functions in PHP

Image
   Array functions in PHP   In this blog I am going to teaching you some of the most useful and frequently used array functions. Array functions in PHP, generally used to manipulate arrays. These are inbuilt in PHP no need to download any other  software. Here you will find some array functions list in the down below. We discuss each and every function in detail with syntax and examples.   Top 10 useful Array functions in PHP     count  sizeof   array_sum   array_pop   array_push   array_rand   unset   in_array   sort   rsort   TopMenu↑ 1. Count function       count : prints count of total no. of elements in an array.    Syntax:     count($array-variable);    ex ample:   <?php   $cart = array ( "pen" , "notebook" , "eraser" );   $n = count ( $cart );   echo " $n Items in the Cart.<br>" ;   for ( $i =0 ; $i < $n ; $i ++ ) { echo "Item" . $i . "=" . $cart [ $i ] . "<br>" ;  }   ?>