Knowledge in php notes

HTTP response code in PHP

How to send HTTP response code in PHP?HTTP Response code: There are three approaches to accomplish the above requirement, depending on the version.For PHP versions 4.0: In order to send the HTTP response code, we need to assemble the response code. To achieve this, use header() function. The header() function contains a special use-case which can detect a HTTP response line and replace that with a custom one.header("HTTP/1.1 200 OK"); Example:$sapitype = php_sapi_name(); if (substr($sapitype, 0, 3) == 'cgi')     header("Status: 404 Not Found"); else    header("HTTP/1.1 404 Not Found"); For PHP versions 4.3: There are obviously a few problems when using first method. The biggest one is that it is partly parsed by PHP and is poorly documented. Since the version 4.3, the header() function has an additional 3rd argument through which we can set the response code. but to use the first argument should be a non-empty string.header(':', true, 400); header(‘X_PHP_Response_Code: 400', true, 400); The second one is recommended. The header field name in this variant is not standardized and can be modified.For PHP versions 5.4: This versions uses http_response_code() function to makes things easier.http_response_code(400); Example: This example uses http_response_code() function to send HTTP response code.<?php   error_reporting(E_ERROR | E_PARSE);   // Initialize a variable into domain name $domain1 = 'https://contribute.geeksforgeeks.org';   // Function to get HTTP response code function get_http_response_code($domain1) {     $headers = get_headers($domain1);     return substr($headers[0], 9, 3); }   // Function call $get_http_response_code = get_http_response_code($domain1);   // Display the HTTP response code echo $get_http_response_code;   // Check HTTP response code is 200 or not if ( $get_http_response_code == 200 )     echo "<br>HTTP request successfully"; else    echo "<br>HTTP request not successfully!";       ?> Output:200 HTTP request successfully

Remove all leading zeros in a string in PHP

How to remove all leading zeros in a string in PHP ?Given a number in string format and the task is to remove all leading zeros from the given string in PHP.Examples:Input : str = 00006665555 Output : 6665555 Input : str = 00775505 Output : 775505 Method 1: Using ltrim() function: The ltrim() function is used to remove whitespaces or other characters (if specified) from the left side of a string.Syntax:ltrim( "string", "character which to be remove from the left side of string"); Program:<?php   // Store the number string with // leading zeros into variable $str = "00775505";   // Passing the string as first // argument and the character // to be removed as second // parameter $str = ltrim($str, "0");           // Display the result echo $str;   ?> Output:775505 Method 2: First convert the given string into number typecast the string to int which will automatically remove all the leading zeros and then again typecast it to string to make it string again.Program:<?php   // Store the number string with // leading zeros into variable $str = "00775505";   // First typecast to int and // then to string $str = (string)((int)($str));           // Display the result echo $str;   ?> Output:775505

PHP program to change date format

PHP program to change date formatYou are given a string which contain date and time. Date in dd/mm/yyyy format and time in 12 hrs format.You have to convert date in yyyy/mm/dd format and time in 24 hrs format.Examples:Input : $date = "12/05/2018 10:12 AM" Output : 2018-05-12 10:12:00 Input : $date = "06/12/2014 04:13 PM" Output : 2014-12-06 16:13:00 First we will convert date to unix timestamp using strtotime() and then use date() to convert it to a specific format(The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT))<?php       // function to convert string and print     function convertString ($date)     {         // convert date and time to seconds         $sec = strtotime($date);           // convert seconds into a specific format         $date = date("Y-m-d H:i", $sec);           // append seconds to the date and time         $date = $date . ":00";           // print final date and time         echo $date;     }           // Driver code     $date = "06/12/2014 04:13 PM";     convertString($date); ?> Output:2014-06-12 16:13:00

PHP and MySQL

PHP consists of a scripting language and an interpreter. Like other scripting languages, PHP enables web developers to define the behavior and logic they need in a web page. These scripts are embedded into the HTML documents that are served by the web server. SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in a relational database. SQL is the standard language for Relational Database System.

Variables,Validations,Arrays in PHP with program.

Variables,Validations,Arrays in PHP with program which can be useful for those who wants to learn web development..