How to Create Function with Multiple Returns in PHP

If you need to create a PHP function that returns multiple values, then you can do one of the following. Option 1 – Returning an array 1 2 3 4 5 6 function returnArray() { return array("one", "two"); } // call the function and print the response var_dump(returnArray()); Option 2 – Returning a conditional 1 2 3 4 5 6 7 function returnConditional($x = true) { if ($x) return "one"; else return "two"; } // call the function with a value and print the response var_dump(returnConditional(false)); Option 3 – Using generator to yield values 1 2 3 4 5 6 7 8 9 function multipleValues() { yield "one"; yield "two"; } $res = multipleValues(); foreach($res as $r) { echo $r; // first val=="one", second val=="two" }

How to Download a File in NodeJS without any Third Party Libraries

If you need to download a file in NodeJS without using any third party libraries, then you can do the following. The NodeJS ecosystem comes with a fs module, this is to denote the FileSystem built in library. First declare your imports: 1 2 const http = require('https'); const fs = require('fs'); Now you can use the https module to download a file, and write it to a stream using fs.

How to Execute a Shell Script in NodeJS

If you need to execute a shell script in NodeJS, then you can use the exec keyword. Syntax: exec(command [, options] [, callback] 1 2 const shell = require('shelljs') shell.exec("npm --version")

How to Print Multiple Arguments in Python

If you need to print out multiple arguments using Python, then you can do one of the following: Option 1 – Using print 1 print("Something", "Else", "Please") Output: Something Else Please Option 2 – Using String Formatting 1 print("Something {} {}".format("Else", "Please")) Output: Something Else Please Or with explicit ordering: 1 print("Something {1} {0}".format("Else", "Please")) Output: Something Please Else Option 3 – Using F-String Formatting 1 2 3 4 5 one = "Something" two = "Else" three = "Please" print(f"{one} {two} {three}") Output: Something Else Please

How to Delete all Resources Except One in Terraform

If you need to delete all resources created by Terraform, except for a single, specific one, then you can do the following: Step 1 – Get the current state list 1 terraform state list Step 2 – Remove the exception resource Remove the specific resource that you don’t want Terraform to track anymore. 1 terraform state rm <resource_to_be_removed> Step 3 – Destroy the resources 1 terraform destroy

How to Copy Files between Two Nodes using Ansible

If you need to copy files between two (2) nodes, using Ansible, then you can do the following: This solution uses the synchronize module, specifically using the delegate_to:source-server keywords. 1 2 3 4 5 - hosts: serverB tasks: - name: Copy Remote-To-Remote (from serverA to serverB) synchronize: src=/copy/from_serverA dest=/copy/to_serverB delegate_to: serverA

How to Copy Multiple Files with Ansible

If you need to copy multiple files using Ansible, then you can do the following: How to Copy Multiple Files with Ansible Look into using the with_fileglob loop as follows: 1 2 3 4 5 6 7 - copy: src: "{{ item }}" dest: /etc/fooapp/ owner: root mode: 600 with_fileglob: - /playbooks/files/fooapp/* If you would like to do it as a task, then this could help: 1 2 3 4 5 6 - name: Your copy task copy: src={{ item.

How to Write Multiline Shell Scripts in Ansible

If you need to write a shell script in Ansible, you probably have something like this: 1 2 3 - name: iterate user groups shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} with_items: "{{ users }}" But how do you write multiline shell scripts with this format? How to write Multiline shell scripts 1 2 3 4 5 6 - name: iterate user groups shell: | groupmod -o -g {{ item['guid'] }} {{ item['username'] }} do_some_stuff_here and_some_other_stuff with_items: "{{ users }}" Just note that Ansible can do some strange things with manipulations of arguments, so you may want to follow something like this:

How to Pass Variables to Ansible Playbook CLI

If you need to pass a variable to Ansible playbook, using the command line, then you can do the following: Option 1 – Specifying command line arguments 1 ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo" N.B. --extra-vars specified variables will override any variables with the same name defined inside the playbook. You can also read up on Passing Variables On The Command Line (Wayback Machine link to maintain versioning) Option 2 – Specify a YML file You can also specify a .

How to Create a Directory using Ansible

If you need to create a directory using Ansible, then you can do the following: Create a Directory in Ansible You will need the file module, then to create a directory you simply specify the option state=directory: 1 2 3 4 - name: Creates a directory file: path: /src/www state: directory Note that with state=directory, all the immediate subdirectories will be created if they don’t already exist. Extending the file module 1 2 3 4 5 6 7 - name: Creates a directory file: path: /src/www state: directory owner: www-data group: www-data mode: 0775 Create the Directories Recursively 1 2 3 4 5 6 7 8 - name: Creates directory file: path: /src/www state: directory owner: www-data group: www-data mode: 0775 recurse: yes This is similar to the recursive argument used with mkdir -p

How to Disable Screensaver on Mac using the CLI

If you want to disable the Screensaver on your Mac, by using the Command Line Interface (CLI), then you can do the following: Step 1 – See what is currently set 1 defaults read com.apple.screensaver Step 2 – Set the idleTime 1 defaults -currentHost write com.apple.screensaver idleTime 0 Step 3 – (Optional) – Undo the change 1 defaults -currentHost delete com.apple.screensaver idleTime

How to Remove Old and Unused Docker Images

If you need to remove any old and unused Docker images, then you can do the following: How to Remove Old and Unused Docker Images Firstly you need to see all the images: 1 docker images You can also use ls to see the Docker Images: 1 docker image ls How to Remove a Single Docker Image The docker rmi command will remove a single Docker image as follows: 1 docker rmi <image_id> You can also use the Docker image names as follows:

How to Convert Milliseconds to Date in Javascript

If you need to convert Milliseconds to Date in Javascript, then you can do the following: How to Convert Milliseconds to Date in Javascript 1 2 let date = new Date(milliseconds); date.toString(); Common Date Conversions From Milliseconds in Javascript 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 let originalDate = new Date(milliseconds); // output: "D/MM/YYYY, H:MM:SS PM/AM" originalDate.toLocaleString(); //output: "D/MM/YYYY" originalDate.

How to Convert String to Title Case in Javascript

If you need to convert a String to Title Case in Javascript, then you can do one of the following: Option 1 – Using a for loop 1 2 3 4 5 6 7 8 function titleCase(str) { str = str.toLowerCase().split(' '); for (var i = 0; i < str.length; i++) str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); return str.join(' '); } console.log(titleCase("this is an example of some text!")); Output: This Is An Example Of Some Text!

How to Compile Multiple Java Files from a Single Command in Java

If you need to compile multiple Java files using a single command, then you can do the following. First, it’s good to learn how to compile a Java file. How to Compile a Java File Let’s take the following Java code: 1 2 3 4 5 class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } To compile this, we simply do the following: 1 javac HelloWorld.java Then when we need to run it, we do:

How to Convert JSON to a Java Object

If you need to convert JSON to a Java Object, then you can do one of the following: Option 1 – Using Gson 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 import com.

How to Calculate Powers of Integers in Java

If you need to calculate the powers of Integers in Java, then you can do one of the following: Option 1 – Using for loops 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Power { public static void main(String args[]){ int number = 5; int power = 3; int result = calculatePower(number,power); System.out.println(number+"^"+power+"="+result); } static int calculatePower(int num, int power){ int answer = 1; if (num > 0 && power==0){ return answer; } else if(num == 0 && power>=1){ return 0; } else{ for(int i = 1; i<= power; i++) answer = answer*num; return answer; } } } Option 2 – Using Recursion 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Power { public static void main(String args[]){ int number = 3; int power = 3; int result = CalculatePower(number,power); System.

How to Get Today’s Date in Java

If you need to get today’s date in Java, then you can do one of the following: Option 1 – Using LocalDate 1 2 3 4 5 6 7 8 import java.time.LocalDate; public class GetTodayDate { public static void main(String[] args) { LocalDate todaysDate = LocalDate.now(); System.out.println(todaysDate); } } Option 2 – Using Calendar and SimpleDateFormat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.

How to Copy Files from Docker Container to Host

If you need to copy files from a Docker Container to the Host, then you can do one of the following: Option 1 – Using docker cp Syntax: 1 docker cp [OPTIONS] CONTAINER: SRC_PATH DEST_PATH Setup the container: 1 2 3 4 5 6 7 8 # pull the ubuntu image docker pull ubuntu # run the container locally docker run -it -d ubuntu # connect to the container docker exec -it abcde123456 /bin/bash Create a file from the container: