Blog

Quick PHP Variable Substitution Example

Here is a quick example of PHP variable substitution:

<?php echo 2 + 2;
  $myname = 'Perry';
?>
		
<h1>This page is all about <?php echo $myname ?></h1>
<?php echo 5 * 5 ?>
<h2>All About <?php echo $myname ?></h2>

The above code snippet assigns ‘Perry’ to the variable $myname, and then uses that variable in the h1 and h2 heading lines.

Basic express.js Development Server Setup

To set up a server using express.js, add the following code to src/index.js:

// index.js
// This is the main entry point of our application

const express = require('express');
const app = express();
const port = process.env.PORT || 4000;

app.get('/', (req, res) => res.send('Hello World!!!'));

app.listen(port, () =>
  console.log(`Server running at http://localhost:${port}`)
);

Start the server, and listen on port 4000, with this command:

node src/index.js

You can now browse to http://localhost:4000, to see Hello World!!!

Any change to the code, will require a restart to the server, and a browser refresh.

To automate this, add the following to your package.json:

"dev": "nodemon src/index.js",

You can now use node to monitor for any changes, with this command:

npm run dev

How to Update npm

Node Package Manager (npm) always seems to have an update available. Fortunately, it’s quick and easy to update.

Display the current npm version, update npm, and check the version again:

npm --version
npm install -g npm
npm --version

Here’s the link to the official npm docs for the update.

You can also download the latest node.js if needed here. It’s recommended to use the LTS version, which has been tested with npm.

How to Install MongoDB

Thanks to my local library, and my kindle, I recently borrowed the O’Reilly e-book “JavaScript Everywhere“, by Adam D. Scott. “Building Cross-Platform Applications with GraphQL, React, React Native, and Electron”… should be interesting, only 344 pages. Seems like quite a bit to cover, we’ll see.

One of the first requirements early in the book, getting MongoDB up and running. MongoDB is used because it stores your data in JSON format.

Installing the MongoDB community edition on your Window’s laptop is fairly straightforward using these steps, although it does take a few minutes.

MongoDB Download
MongoDB Download
  1. Search for MongoDB download center. Select the link for the MongoDB Community Download.
  2. Click the Download button on the right side of the page. The defaults are likely ok.
  3. Click Open.
  4. Accept the terms.
  5. Choose Setup Type: Complete.
  6. Leave the defaults to Install MongoDB as a Service, click Next.
  7. Leave the default checked to install Compass.
  8. Click Install.

After several minutes, you are good to go. MongoDB is now running, and a MongoDB Compass window should be open.

MongoDB Compass
MongoDB Compass

Adding a Search Icon to your WordPress Menu

Ivory Search Plugin
Ivory Search Plugin

For a simple way to add a search button to your WordPress menu, use the Ivory Search plugin. It provides all sorts of controls, including the “Sliding” option which I selected.

Here’s a great article on adding search, and the options to do so either through a plugin or custom PHP code.

Easy Changing the Favicon Icon in WordPress

The site favicon can be easily changed in WordPress. The favicon is the site icon shown in the browser tab, bookmarks, etc.

Create a Favicon

The favicon should be a PNG file, which is a 512 x 512 size for WordPress. You can create this using software such as Photoshop, or if you are looking for something simple, use a Favicon Generator. With favicon.io, which I used, it is very easy.

  1. Go to their website.
  2. Select the Text -> ICO option (the second one… they also do image and emoji).
  3. Type in your text, select font, colors, size, etc. until you get it the way you like.
  1. Download the zip file, extract the zip.
  2. They provide output in a number of sizes, for WordPress the file android-chrome-512×512.png will work well.

Install the Favicon

On my version of WordPress this procedure worked.

  1. Select Dashboard -> Appearance -> Customize -> Site Identity
  2. At the bottom of the menu on the left, there is an option to upload the icon, or change it.
  3. Upload the 512×512 sized icon from above.
  4. Click Publish. Should be all set!

A helpful blog article on this can be found from wordpress.com here, however I did find that the menu location for the upload was different with my site’s theme and version.

WordPress Site Migration Issue with Max Upload Size

I ran into an issue when attempting to migrate a WordPress site from local to a new AWS Lightsail instance using the popular All-in-One WP Migration plugin.

Normally this plugin works great… use it to perform the usual Export and then Upload / Import it onto the new site. This time, not so much.

Continue reading “WordPress Site Migration Issue with Max Upload Size”

Working with Big Numbers in JavaScript

Need to work with a really big number in JavaScript? Larger than a normal int? BigInt is your answer!

Say, for example, you were trying to solve the “A Very Big Sum” problem on HackerRank. The code is simple with BigInt():

function aVeryBigSum(ar) {
    // Write your code here
    let sum = 0n;

    for (let i = 0; i < ar.length; i++) {
        sum = sum + BigInt(ar[i]);
    }
    return sum;
}

You can find detail on Mozilla and W3docs.