Sunday, 21 June 2020

isset function

<?php 
    $var1 = 'test'; 
    if(isset($var1)) { 
        echo "The variable $var1 is set, so it will print. </br>"; 
        var_dump(isset($var1)); 
    } 
?> 



output


The variable test is set, so it will print.
bool(true)




  1. <?php  
  2.     $x = 9;  
  3.     //TRUE because $x is set  
  4.     if ($resultX = isset ($x)) {  
  5.         echo "Variable 'x' is set.";  
  6.     }  
  7.     echo var_dump(isset($resultX)). "</br>";  
  8.       
  9.     $y = NULL;  
  10.     //False because $y is Null  
  11.     if (isset ($y)) {  
  12.         echo "Variable 'y' is not set.";  
  13.     }  
  14.     echo var_dump(isset($y)). "</br>";  
  15.       
  16.     $z ='\0';  
  17.     //TRUE because \0 and NULL are treated different  
  18.     if ($resultZ = isset ($z)) {  
  19.         echo "Variable 'z' is set.";  
  20.     }  
  21.     echo var_dump(isset($resultZ));  
  22. ?>  

var_dump

echo


  • Outputs one or more strings separated by commas
  • No return value
    e.g. echo "String 1", "String 2"
print
  • Outputs only a single string
  • Returns 1, so it can be used in an expression
    e.g. print "Hello"
    or, if ($expr && print "foo")
print_r()
  • Outputs a human-readable representation of any one value
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • Useful when debugging
  • May return its output as a return value (instead of echoing) if the second optional argument is given
  • There are two more functions similar to print_r() which are var_export(), and var_dump(). They display the private and protected properties of objects.
  • print_r (mixed $var_name, boolean $return_output)  
var_dump()
  • Outputs a human-readable representation of one or more values separated by commas
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • Uses a different output format to print_r(), for example it also prints the type of values
  • Useful when debugging
  • No return value
var_export()
  • Outputs a human-readable and PHP-executable representation of any one value
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • Uses a different output format to both print_r() and var_dump() - resulting output is valid PHP code!
  • Useful when debugging
  • May return its output as a return value (instead of echoing) if the second optional argument is given







var_dump


hat dumps the information about the variables.


<?php  
    //PHP program to demonstrate the working of var_dump function  
    $x = 25;  
    //dump integer variable  
    var_dump ($x);            
    echo "</br>";  
      
    $y = 32.5;  
    //dump float variable  
    var_dump ($y);            
    echo "</br>";  
      
    $bvalue = true;  
    //dump boolean variable  
    var_dump ($bvalue);  
?>  



int(25)
float(32.5)
bool(true)








<?php  
    //dump strings  
    $msg1 = "Hello Alex";  
    var_dump ($msg1);  
      
    echo "</br>";  
    $msg2 = "Welcome to javatpoint";  
    var_dump ($msg2);  
?>  



string(10) "Hello Alex"
string(21) "Welcome to javatpoint"

how to get the current page url in php

  1. <?php  
  2.     if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')   
  3.          $url = "https://";   
  4.     else  
  5.          $url = "http://";   
  6.     // Append the host(domain name, ip) to the URL.   
  7.     $url.= $_SERVER['HTTP_HOST'];   
  8.     
  9.     // Append the requested resource location to the URL   
  10.     $url.= $_SERVER['REQUEST_URI'];    
  11.       
  12.     echo $url;  
  13.   ?>   

or


  1. <?php  
  2. $protocol = ((!emptyempty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";  
  3. $CurPageURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];  
  4. echo "The URL of current page: ".$CurPageURL;  
  5. ?>    







to get the current page url




  1. <?php  
  2.     $curPageName = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);  
  3.     echo "The current page name is: ".$curPageName;  
  4.     echo "</br>";  
  5.   ?>   





new line starts php

  1. <?php  
  2.     echo nl2br("New line will start from here\n in this string\r\n on the browser window");  
  3. ?>  



New line will start from here
in this string
on the browser window



  1. <?php  
  2.     echo "One line after\n another line";  
  3.     echo " One line after\r\n another line";  
  4. ?>  
One line after another line One line after another line

Friday, 19 June 2020

php mail local host server

smtp server
open the file from sendmail
sendmail.ini
smtp_server=mail.mydomain.com smtp.gmail.com get the smtp server
change to gmail server
smtp_port=25 587 change to port 
smtp_ssl=auto tls change the ssl
auth_username= emailid write the emial id 
auth_password= password write the password
force_sender= emailid write theemial id
hostname= localhost write the host
uncomment ;error_logfile=error.log to error_logfile=error.log create the error.log file
uncomment ;debug_logfile=debug.log to debug_logfile=debug.log create the debug.log file
php.ini
xampp/php/php.ini
[mail function]
; For Win32 only. ; For Win32 only.
; http://php.net/smtp ; http://php.net/smtp
SMTP=localhost ;SMTP=localhost comment the local host
; http://php.net/smtp-port ; http://php.net/smtp-port
smtp_port=25 ;smtp_port=25 comment the port
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from =  ;sendmail_from =  coment the sendmail from
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path = sendmail_path = C:\xampp\sendmail\sendmail.exe
extension=php_openssl.dll check this enabls or not checks theis
check in errror log
debug
myaccountsignin
allowlesssecureapps
<?php  
   $to = "recievver mailid";//change receiver address  
   $subject = "hai vamsi";  
   $message = "This is simple text amessage.";  
   $header = "From:vamsikrishnaengineer@gmail.com \r\n";  
  
   $result = mail ($to,$subject,$message,$header);  
  
   if( $result == true ){  
      echo "Message sent successfully...";  
   }else{  
      echo "Sorry, unable to send mail...";  
   }  
?>  







Thursday, 18 June 2020

php include and require

pho include and require
in php page, if require file is not found, it wil stop the execution of remaining and include - if include file is not found, it wll generete error and execute remaing script
include only generates a warning, i.e., E_WARNING, and continue the execution of the script.
require generates a fatal error, i.e., E_COMPILE_ERROR, and stop the execution of the script.
1. include 'filename ';  
2. Or   
3. include ('filename');  
php requirre
PHP require is similar to include, which is also used to include files. The only difference is that it stops the execution of script if the file is not found whereas include doesn't.
1. require 'filename';  
2. Or   
3. require ('filename');  



php forms

php get form
form1.html
1. <form action="welcome.php" method="get">  
2. Name: <input type="text" name="name"/>  
3. <input type="submit" value="visit"/>  
in html page , whenever it submits, it redircts to welcome.pho
4. </form>  
welcome.php
1. <?php  
2. $name=$_GET["name"];//receiving name field value in $name variable  
it gets the value
3. echo "Welcome, $name";  
it prints the value
4. ?>  
get willl show in url
php post form
form1.html
1. <form action="login.php" method="post">   
2. <table>   
3. <tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>  
post will not show in url
4. <tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>   
5. <tr><td colspan="2"><input type="submit" value="login"/>  </td></tr>  
6. </table>  
7. </form>   
login.php
1. <?php  
2. $name=$_POST["name"];//receiving name field value in $name variable  
3. $password=$_POST["password"];//receiving password field value in $password variable  
4.   
5. echo "Welcome: $name, your password is: $password";  
6. ?>