Programming Articles - Page 2039 of 3363

How to identify server IP address in PHP?

AmitDiwan
Updated on 06-Apr-2020 07:31:25

4K+ Views

The server IP can be identified with the below line of code −$_SERVER['SERVER_ADDR'];The port can be identified using the below line of code −$_SERVER['SERVER_PORT'];For PHP version 5.3 and higher, the following lines of code can be used −$host_addr= gethostname(); $ip_addr = gethostbyname($host_addr);This can be used when a stand-alone script is being run (which is not running via the web server).

How to force file download with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:30:32

2K+ Views

The below code can be used to force a file to get downloaded in PHP.

Can HTML be embedded inside PHP “if” statement?

AmitDiwan
Updated on 06-Apr-2020 07:27:27

6K+ Views

Yes, HTML can be embedded inside an ‘if’ statement with the help of PHP. Below are a few methods.Using the if condition −    it is displayed iff $condition is met Using the if and else if conditions −     it is displayed iff $condition is met    HTML TAG HERE    HTML TAG HERE Embedding HTML inside PHP − HTML TAG HERE

How to detect search engine bots with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:21:13

1K+ Views

A search engine directory of the spider names can be used as a reference. Next, $_SERVER['HTTP_USER_AGENT']; can be used to check if the agent is a spider (bot).Below is an example demonstrating the same −if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "some_bot_name")) {    //other steps that need to be used }Code explanation − The agent, along with the user agent is passed to the strtolower function, whose output in turn is passed to the strstr function. Both the user agent and the bot are compared to see if the spider is a bot or not.Another option is shown below −function _bot_detected() {    return (   ... Read More

In PHP, can you instantiate an object and call a method on the same line?

AmitDiwan
Updated on 06-Apr-2020 07:06:10

944 Views

Yes, an object can be instantiated and a method can be called on a single line using PHP. This feature has come into effect beginning from PHP version 5.4.An object can be instantiated by accessing the class member of the class. This can be seen in the below snippet −(new my_var)-> my_instance()Code explanation − Here, my_instance is the method and my_var is the object that needs to be instantiated.Example Live Democlass Test_class {    public function __construct($param) {       $this->_var = $param;    }    public function my_method() {       return $this->_var * 2;    }   ... Read More

Print newline in PHP in single quotes

AmitDiwan
Updated on 06-Apr-2020 07:03:11

676 Views

Since can’t be used with single quotes, we need to resort to other options.When using command line interface, the constant PHP_EOL can be used.When using with browsers, the ‘’ can be used.Both the options have been demonstrated below.Suppose our option was not cli, the ‘else’ part will be executed and a newline will be printed −Example Live DemoOutputThis will produce the following output −hi hello hihello

How can I break an outer loop with PHP?

AmitDiwan
Updated on 06-Apr-2020 06:59:59

253 Views

If there are two nested loops, the break statement can be used −break 2;Below is a demonstration with the foreach loop −foreach(...) {    foreach(...) {       if (my_var_1.name == my_var_2)       break 2; //it breaks out of the outermost foreach loop    } }For PHP version>=5.3, the below lines of code can be used −foreach (...) {    foreach (...) {       if (my_var_1.name == my_var_2)       goto top;    } } top:

How to print previously typed snippets in JShell in Java 9?

raja
Updated on 03-Apr-2020 17:07:48

95 Views

JShell is an official Read-Evaluate-Print-Loop (REPL) introduced in Java 9. It provides an interactive shell for quickly prototyping, debugging, and learning Java and Java API without the need for a main() method.The "/list" command in JShell prints out all of the previously typed snippets of that particular session with a unique identifier called the snippet ID. By default, the output doesn't contain any snippet with only valid statements or expressions that can be shown. We need to see all previously typed code include errors, then pass the -all argument to the /list command.In the below code snippet, we have created ... Read More

What are the changes in Memory Management in Java 9?

raja
Updated on 03-Apr-2020 14:09:51

330 Views

Garbage Collection or simply GC is the core part of Memory Management in Java. It can be responsible for cleaning dead objects from memory and reclaiming that space. GC executes cleanup using predefined Garbage Collectors that uses certain algorithms.There are a few important types of Garbage Collectors listed belowSerial GC: A single thread collector and applies to small applications with small data usage. It can be enabled by specifying the command-line option: -XX:+UseSerialGC.Parallel GC: Parallel GC uses multiple threads to perform the garbage collecting process, and it's also known as the throughput collector. It can be enabled by explicitly specifying the option: -XX:+UseParallelGC.G1 Garbage First:  G1 (Garbage ... Read More

How can we import a gson library in JShell in Java 9?

raja
Updated on 02-Apr-2020 16:46:03

421 Views

Java 9 introduced an interactive REPL command-line tool named JShell. It allows us to execute Java code snippets and get immediate results. We can import external classes that can be accessed from a JShell session through the classpath. The Gson library is a Java serialization/deserialization library intended for converting Java Objects into JSON and vice-versa.In the below code snippet, we can set the classpath in JShelljshell> /env --class-path C:\Users\User\gson.jar | Setting new options and restoring state.Once we have imported the gson library in JShell, able to see that library in the list.jshell> import com.google.gson.* jshell> /import | import java.io.* | import java.math.* | ... Read More

Advertisements