<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dipu Raj on Medium]]></title>
        <description><![CDATA[Stories by Dipu Raj on Medium]]></description>
        <link>https://medium.com/@dipuraj?source=rss-311233931c99------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gZn5MqjX86p_ICrpNcVPlA.jpeg</url>
            <title>Stories by Dipu Raj on Medium</title>
            <link>https://medium.com/@dipuraj?source=rss-311233931c99------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 06 May 2026 19:38:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dipuraj/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Deploying a Node.js web app on Amazon EC2 with Apache server]]></title>
            <link>https://medium.com/@dipuraj/deploying-a-node-js-web-app-on-amazon-ec2-with-apache-server-b56d5a546eb0?source=rss-311233931c99------2</link>
            <guid isPermaLink="false">https://medium.com/p/b56d5a546eb0</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[apache]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Dipu Raj]]></dc:creator>
            <pubDate>Thu, 19 Mar 2020 06:59:12 GMT</pubDate>
            <atom:updated>2022-07-08T14:58:22.920Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deploying a Node.js Web Application on Amazon EC2 with Apache</h3><h4>Deploy your Node.js web application on AWS EC2 instance with Apache web server</h4><figure><img alt="AWS Node.js Apache PM2" src="https://cdn-images-1.medium.com/max/750/1*HggP6ld0Bd-7akuf2laJJg.png" /></figure><p>When you are done with your Node.js web application the next big thing you may worry about is “how and where to deploy it?”. There are many ways to deploy a Node.js application. Most favorite option is managed hosting like Heroku, DigitalOcean, AWS Elastic Beanstalk, etc. But you can also host Node.js applications on your own server and manage yourself.</p><p>In my case, I had an AWS EC2 server with existing Laravel applications running. And my goal was to deploy the Node.js application on it without messing the existing Laravel sites and with minimal installs possible. So I decided to use the existing Apache server and didn’t want to install another web server like Nginx. Fortunately, everything went well and I had my application deployed without messing anything. With that experience in hand, here I am trying to explain the step by step process to deploy a Node.js web application on Amazon EC2 as simple as I can. And it will be same for all Linux servers you have.</p><p>To get a clear picture of what we are doing, we can split the steps into 3 major sections.</p><ul><li><strong>Step A:</strong> Install Node.js and setup your application on EC2 instance.</li><li><strong>Step B:</strong> Install a process manager and keep your application run always.</li><li><strong>Step C:</strong> Configure Apache to work with the Node.js application.</li></ul><h3>Step A: Install Node.js and setup your application</h3><p>In this step, we will install the Node.js and setup your project on your EC2 instance. Assume you have root SSH access to your EC2 instance and access to your project source code. All the steps are doing inside the Linux terminal, so go ahead and log in to your server.</p><pre>ssh &lt;username&gt;@&lt;server ip&gt;</pre><h4>Step A-1: Install Node.js</h4><p>The best and easy way is to install Node.js is using <a href="https://github.com/nvm-sh/nvm">NVM</a> (Node Version Manager).</p><p>Download and install <a href="https://github.com/nvm-sh/nvm">NVM</a>.</p><pre>curl -o- <a href="https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh">https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh</a> | bash</pre><p>Reload the profile.</p><pre>source ~/.profile</pre><p>Now install Node.js. You must be aware of the node version that supported by your project or install the latest LTS version.</p><pre>nvm install v10.19.0</pre><h4>Step A-2: Clone the project</h4><p>This is a simple step to copy your project to the server. You can either download or clone from a repository. Usually, the web projects are saved under the folder “/var/www/html”, so you can follow that.</p><pre>cd /var/www/html<br>git clone &lt;repository url&gt;</pre><p>Change the directory to your project directory</p><pre>cd &lt;your new project directory&gt;</pre><h4>Step A-3: Install dependencies and build</h4><p>Now you can install your project dependencies and build your project.</p><pre>npm install</pre><p>You can also use the “ — production” flag if you don’t want to install the dev dependency. If your project needs to build, you can run you build script, like “npm start” or “npm run build” or “grunt” or “gulp” depends upon your project configuration.</p><h4>Step A-4: Set the environment variable</h4><p>You may need to set the flag `NODE_ENV` to tell your project that it is running on the production environment. We can configure that as a server-level environment variable by editing or creating the file “/etc/environment”</p><pre>sudo vim /etc/environment</pre><p>This will open the file on the vim editor. Add the following line, save and exit (on vim editor, ESC + : + wq + Enter).</p><pre>NODE_ENV=production</pre><p>Logout and login the terminal again to take effect on the environment variable.</p><h4>Step A-5: Verify the application is working</h4><p>Now, make sure that your application is running properly on the server. You can run your project’s start command to check that. If it works, we are halfway succeeded. But if not, you have to check what is missing on the server than your local running environment.</p><pre>node app.js</pre><h3>Step B: Install process manager and keep your application run always</h3><p>Now we are entering into the deployment section. To give you an idea of what we are going to do. The Node.js web applications are started by running a command like “node app.js”. So the web application can listen to the specified port for HTTP requests, for example, “<a href="http://127.0.0.1:3000">http://127.0.0.1:3000</a>”. But, if you exit the remote terminal the command will eventually stop and the project will not be accessible.</p><p>To avoid this problem, we are using a process manager which will keep running the node application even when we exist the remote terminal or the server is restarted.</p><h4>Step B-1: Install the Process Manager</h4><p>We can install the process manager <a href="https://www.npmjs.com/package/pm2">PM2</a>.</p><pre>npm install -g pm2</pre><h4>Step B-2: Configure process</h4><pre>pm2 start <strong>app.js</strong><br>pm2 save<br>pm2 startup</pre><p>Now you are running your application. Please note the above start command. The last parameter is the name of your start script. For example, if you start your application like “node <strong>app.js</strong>” then it will be ”pm2 start <strong>app.js</strong>”.</p><p>You can see the running process and mange it anytime you want. The below command will list the running processes.</p><pre>pm2 list</pre><h4>Step B-3: Test the application is accessible</h4><p>Now we believe the application is running and we must make sure it is happening. We are running the application inside the EC2 server and it is only accessible locally until we configure the web server. We can use telnet command to check if the application is accessible inside the server. (Considering the port configuration for you project is 3000).</p><pre>telnet 127.0.0.1 3000</pre><p>If you get the success response, we are pretty close to the final step.</p><h3>Step C: Configure Apache to work with the Node.js application</h3><p>Now the final step is to route your original domain to the locally running Node.js application (Ex. <a href="http://127.0.0.1:3000">http://127.0.0.1:3000</a>). For this, we can use Apache as a reverse proxy server which will take care of the communication in between.</p><p>Considering you already have made the DNS settings for your domain to point to this EC2 server.</p><h4>Step C-1: Enable Apache proxy modules</h4><p>Enable the Apache proxy modules.</p><pre>sudo a2enmod proxy<br>sudo a2enmod proxy_http</pre><h4>Step C-2: Add virtual host settings</h4><p>Open the Apache virtual host configuration file.</p><pre>sudo vim /etc/apache2/sites-enabled/<strong>000-default.conf</strong></pre><p>Add the following virtual host settings and save the file. Please replace “yourproject.com” with your domain name.</p><pre>&lt;VirtualHost *:80&gt;<br>    ServerName <strong>yourproject.com</strong></pre><pre>    ProxyRequests Off<br>    ProxyPreserveHost On<br>    ProxyVia Full</pre><pre>    &lt;Proxy *&gt;<br>        Require all granted<br>    &lt;/Proxy&gt;</pre><pre>    ProxyPass / <a href="http://127.0.0.1:3000/"><strong>http://127.0.0.1:3000/</strong></a><br>    ProxyPassReverse / <a href="http://127.0.0.1:3000/"><strong>http://127.0.0.1:3000/</strong></a><br>&lt;/VirtualHost&gt;</pre><p>If you have to setup the SSL configuration, you have to add the virtual host settings on the file “<strong>/etc/apache2/sites-enabled/default-ssl.conf</strong>” also.</p><h4>Step C-3: Restart Apache web server</h4><p>Now restart the server to take effect on the virtual host settings.</p><pre>sudo service apache2 restart</pre><p>And we are done! Take the project URL on a browser and see it working.</p><h3>Conclusion</h3><p>Hope everything went well for you too and you have your domain working perfectly. The configuration is not that hard if we do it properly. The best bet is to understand each step and why we are doing it. Hope I have explained the steps properly and easily.</p><p>Thank you so much for reading. Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b56d5a546eb0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A novice’s introduction to Functional Programming]]></title>
            <link>https://medium.com/@dipuraj/a-novices-introduction-to-functional-programming-cae0a8c76b4?source=rss-311233931c99------2</link>
            <guid isPermaLink="false">https://medium.com/p/cae0a8c76b4</guid>
            <category><![CDATA[programming-paradigms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Dipu Raj]]></dc:creator>
            <pubDate>Sat, 18 Nov 2017 21:53:09 GMT</pubDate>
            <atom:updated>2020-04-24T03:56:16.602Z</atom:updated>
            <content:encoded><![CDATA[<h4>A simple and practical introduction to Functional Programming</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*N5IF21K0NPr2KNCr.png" /></figure><p>I was struggling for many months just to understand what really is <em>Functional Programming</em>. I found it very complex and insane in my early days of learning. Then I realise that it was not the problem with functional programming but was mine. I was doing a mistake of learning it heavily comparing with <em>Object Oriented Programming</em> which I am comfortable with. Just like they say <em>“comparing apples and oranges”</em>.</p><p>After I realised my mistake, I learned it with a fresh perspective. And surprisingly it was getting into my mind like a magic. Now, I wouldn’t say I am a hardcore functional programmer (or even a functional programmer, I haven’t really worked on any pure functional programming languages!). But, I was able to understand the concepts of functional programming. Based on my encounter with functional programming, here I am trying to explain the functional programming as simple as I can.</p><h3>What is Functional Programming?</h3><p>We are familiar with <em>Object Oriented Programming</em> and most of us use that approach to write our daily dose of programs. Similar to that <em>Functional Programming</em> is another paradigm which defines certain ways of writing a program. So it is basically a style of programming.</p><p>Functional programming is inspired by mathematical functions, specifically from lambda calculus, a formal system developed in 1930 by Alonzo Church to represent computation.</p><h4>Why does Functional Programming get popular these days?</h4><p>Functional programming is invented around 60 years back (First functional programming language is LISP invented in 1958), but it was very rarely used for software development. For the last few years, it becomes very hot topic and you can see many articles, talks, and videos about it.</p><p>There may be several reasons and the major reason we can think of is the need for <em>concurrent programming</em>. Since the internet is growing rapidly, high-end processing power is needed to handle the different process and the vast amount of data. Concurrent programming is effectively used to split the processing power into a different core or even different systems. Functional programming can support concurrent programming with great reliability. So big players like Facebook, Amazon, Twitter etc. are using functional programming languages to write their heavy processes.</p><p>Another reason would be <em>JavaScript</em>. JavaScript has good support for functional programming even if its a multi-paradigm language. So JavaScript developers started writing programs in the functional way recently. You can see many functional programming talks is by JavaScript developers(like me!) even though they haven’t worked on any pure functional programming languages!</p><h3>Functional Programming Concepts</h3><p>Just like we have <em>Object Oriented Concepts</em> (like Inheritance, Polymorphism etc.), functional programming has it’s own concepts too. Let’s discuss a few basic concepts of Functional Programming.</p><h4>Pure functions</h4><p>Pure functions are normal functions with a few additional rules.</p><ul><li>It should take parameter(s) and should return a value</li><li>It should not access or modify any global variable</li><li>It should not have side effects (writing to a file, reading database etc.)</li></ul><p>Pure functions always return the same result for the same set of inputs, because it only processes its parameters and returns the result. It doesn’t need any language support to write it because the rules are very basic. But some pure functional programming languages force the developer to write pure functions since functional programming states that all the functions of a program should be <em>“pure functions”!</em></p><pre>// PURE FUNCTION EXAMPLE <br>function sayHello(greet, name) { <br>    return greet + &quot; &quot; + name; <br>}</pre><pre>alert(sayHello(&quot;Hello&quot;, &quot;Sam&quot;)); <br>// Output: Hello Sam</pre><h4>Lambda functions</h4><p>Lambda functions or anonymous functions are functions without a name. Lambda functions are building block for many of the concepts in functional programming. See an example of a lambda function.</p><pre>function (a, b) { <br>    return a + b; <br>};</pre><h4>First-class functions</h4><p>This is a programming language feature. If a programming language supports first-class functions then it treats functions as <em>first-class citizens</em>.</p><p>For example, in normal programming language what we can do with functions? We can <em>“define a function”</em> and <em>“call a function”</em>, that’s all. But in the case of a data types (string, int, double etc.) we can do many more things like…</p><ul><li>Assign values to it</li><li>Pass it to a function as argument</li><li>Get it as a return type from a function</li></ul><p>Consider we can do all of this with a function too, then we can say the language has <em>“First-class Functions”</em> and we can…</p><ul><li>Pass functions as arguments to other functions</li><li>Return functions as the values from other functions</li><li>Store functions in data structures</li></ul><p>Here is an example of assigning a function to a variable</p><pre>var fnSum = function (a, b) { <br>    return a + b; <br>};</pre><h4>Higher-order functions</h4><p>Higher-order functions are a type of function which does at least one of the following.</p><ul><li>Accept other functions as arguments</li><li>Return other functions as results</li></ul><p>To write higher-order functions the language need to support <em>First-class functions</em>. Following is an example function which returns another function as result of a function.</p><pre>function foo() { <br>    return function() { <br>        return &quot;Hey there!&quot;; <br>    }; <br>} </pre><pre>var bar = foo(); <br>console.log( bar() ); // Hey there!</pre><p>This is the famous syntax of jQuery document ready function, not sure if you have noticed it before, but <em>“ready”</em> is a higher-order function which accepts a function as it’s argument!</p><pre>$(document).ready( function () { <br>    //do some stuff <br>});</pre><h4>Immutable data</h4><p>In functional programming modifying the value of a variable is considered as a serious sin! The reason is to improve readability and run-time efficiency of a program and make it thread-safe. So it recommends the use of immutable data type whose value cannot be modified after it is created. If you want to change the value you need to create a new variable and assign the new value, the existing value cannot be modified.</p><pre>int i = 42; // Initializing value </pre><pre>i = 43; // re-assigning the value is not allowed <br>i++; // modify the value is not allowed <br>i = i + 1; // re-assigning the value is not allowed</pre><h4>Tail recursion</h4><p>We know recursion is a function calling itself. But what is <em>Tail Recursion</em>?<br> It just adds a simple specification to recursion, <strong><em>the recursion call should be at the end of the recursive function.</em></strong> So there is a guarantee that nothing is left to execute in the function after a recursive call. <br> Here is an example of a <em>tail recursive</em> function. You can notice that the last thing does in this function is the recursive call factorial(n - 1)</p><pre>function factorial(n) {<br>    if (n &lt;= 1) return 1;<br><br>    return factorial(n - 1) * n;<br>}<br><br>var fact = factorial(4); // 24</pre><p>And here is the same function without <em>tail recursion</em>. Here you can notice that there is some more calculation is doing after the recursive call, that make the function not a <em>tail recursive</em> function.</p><pre>function factorial(n) {<br>    if (n &lt;= 1) return 1;<br><br>    var fc = factorial(n - 1);<br>    return n * fc;<br>}<br><br>var fact = factorial(4); // 24</pre><p>But why the <em>tail recursion</em> is so important for functional programming? Functional programming recommends the use of recursion instead of loops, because loops are bound with mutation of the variable. But recursion has a performance issue, it creates call stacks for every recursive call and that burns the memory. And if we use <em>tail recursion</em>, it can use one call stack for all the recursive calls of a recursive function and that optimise the performance.</p><h4>Lazy evaluation</h4><p>Lazy evaluation is an evaluation strategy which delays the evaluation of an expression until its value is used. Strict evaluation always fully evaluates function arguments before invoking the function. Lazy evaluation does not evaluate function arguments unless their values are used. <br> Let’s see the following 3 examples. We have a function getLength() which returns the count of elements in an input array. The same function is written in 3 different languages JavaScript, PHP, and C#. While we execute the program these are the result we get...</p><ul><li><strong>JavaScript</strong> =&gt; Gives correct output</li><li><strong>PHP</strong> =&gt; Gives output, but with a warning</li><li><strong>C#</strong> =&gt; Shows error and execution terminates</li></ul><p><strong>JavaScript</strong></p><pre>function getLength(arr) {<br>    return arr.length;<br>}<br><br>var len = getLength([1/2, 40, 2/0, 56]);<br><br>console.log( &#39;Length: &#39; + len );</pre><p><strong>PHP</strong></p><pre>function getLength($arr) {<br>    return count($arr);<br>}<br><br>$len = getLength([1/2, 40, 2/0, 56]);<br><br>echo &#39;Length: &#39;, $len;</pre><p><strong>C#</strong></p><pre>public int getLength(int[] arr) {<br>    return arr.Length;<br>}<br><br>int len = this.getLength(new int[]  { 1/2, 40, 2/0, 56 });<br><br>Console.WriteLine(&quot;Length: &quot; + len);</pre><p>Why PHP and C# show errors? Why didn’t JavaScript show an error?<br> The reason is an element (2/0) in the input array is invalid, the division by zero is indeterminate! That gives the answer why PHP and C# show errors, but why not JavaScript? The reason is the function never uses the value of the elements of the input array, so JavaScript never validated the parameter. That is called <em>Lazy Evaluation</em>. And if we change the function to use any of the elements, then JavaScript also starts to show error.</p><h3>What more in Functional Programming?</h3><p>There is more advanced topic in functional programming. To avoid unnecessary confusion I would recommend to learn them only after you through with the previous topics. These are few of those topics you can add to your bucket list.</p><ul><li>Referential transparency</li><li>Currying</li><li>Closure functions</li><li>Monads</li><li>Map, Reduce, and Filter</li><li>Functional compositions</li></ul><h3>Why should I learn Functional Programming?</h3><p>That’s a pretty obvious question. Many of us try to learn <em>functional programming</em> because everybody is talking about it and we don’t want to be the odd guy. Yeah, that’s a good reason, but there are other valid reasons too.</p><ul><li>Many languages are adopting functional programming features. And you can catch them easily if you already know the theory.</li><li>Many languages are already using functional programming like syntax, for example, LINQ in C#, Array functions (Map, Reduce and Filter). And it makes more sense to your daily programming.</li><li>Functional programming handles a problem in a different way than object oriented programming. So you get new ways to look at the problems.</li></ul><h3>Functional Programming languages</h3><p>Many languages support functional style programming even though they do not strictly follow the functional programming concepts. But there are languages that strictly follow the functional programming concepts too. Here are some list of major functional programming languages…</p><ul><li>Clojure</li><li>Haskell</li><li>Erlang</li><li>Lisp</li><li>Scala</li><li>Scheme</li><li>F#</li><li>OCaml</li></ul><h3>Conclusion</h3><p>Functional programming receives a lot of criticism as it has cryptic concepts. But understand the functional programming concepts is not a big deal if you take the right path. It is simply another way of writing a program, no more than that. Also, there is many fights and talk that say <em>“functional programming is the holy way of programming”</em> (probably because it has pure functions!). And I would say that’s a myth! every programming has its own advantage and disadvantages. At the end, everything is just a compiled bytes!!!</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cae0a8c76b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[jQuery plugin development with Gulp automation]]></title>
            <link>https://medium.com/@dipuraj/jquery-plugin-development-with-gulp-automation-ae5d2c180f85?source=rss-311233931c99------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae5d2c180f85</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[gulp]]></category>
            <category><![CDATA[automation]]></category>
            <dc:creator><![CDATA[Dipu Raj]]></dc:creator>
            <pubDate>Sat, 09 Sep 2017 09:58:05 GMT</pubDate>
            <atom:updated>2020-07-24T05:11:16.194Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>Create a well structured and automated jQuery plugin development environment with Gulp</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*wjX5h8VFl3My01YS.png" /></figure><p>In this article, I am trying to explain the step by step process to setup a jQuery Plugin development environment with modern development practice. It will improve the efficiency and maintainability of the project since we can automate tasks like lint, test, build, commit and publish.</p><blockquote><strong>TL;DR</strong></blockquote><blockquote>You can use the <a href="https://www.npmjs.com/package/create-jquery-plugin">create-jquery-plugin</a> CLI tool to create jQuery Plugins by running npx create-jquery-plugin . Or, you can clone the repository <a href="https://github.com/techlab/jquery-plugin-boilerplate">jquery-plugin-boilerplate</a>.</blockquote><p>Our aim is to…</p><ul><li>Create a nicer directory structure for the jQuery Plugin project.</li><li>Use Gulp to automate tasks lint, transpile, beautify and minimize.</li></ul><p>The <a href="https://github.com/techlab/jquery-gulp-example">example project</a> we use for this article is so simple and minimal. You can use it as a boilerplate for your new plugin and get started.</p><p>See the jquery-plugin-boilerplate project on GitHub</p><h4>Prerequisites</h4><p>You need <a href="https://nodejs.org/">Node</a> installed on your development machine, if not, please download and install from <a href="https://nodejs.org/">here</a></p><h3>1. Directory Setup for the project</h3><p>We can use the latest trending directory structure src and dist. The src is the alias for source and dist for distribution. All our development and coding is happening on src directory and dist directory remains untouched by humans. The dist directory will contain optimized files for the production. Gulp and its plugins are responsible for managing and maintaining the dist directory for us.</p><p>Also, we can add examples directory for the demo and usage examples, usually the HTML files. If you want to do unit testing, the test directory can contain your test scripts.</p><p>Our final directory structure will look like the following, you can create the directory structure in your project directory.</p><pre>- dist/ <br>    - css/ <br>    - js/ <br>- examples <br>- src/ <br>    - css/ <br>    - js/ <br>- test</pre><h3>2. Install Gulp and other dependencies</h3><p>Follows the list of <a href="https://nodejs.org/">Node</a> tools we are going to use in our example project. These are development dependencies and only used in the development environment. You don’t have to install or bundle them on production.</p><ul><li><a href="http://gulpjs.com/">Gulp</a> — a toolkit for automating painful or time-consuming tasks in your development</li><li><a href="https://babeljs.io/">Babel</a> — a JavaScript compiler</li><li><a href="http://jshint.com/">Jshint</a> — a tool that helps to detect errors and potential problems in your JavaScript code</li><li><a href="https://www.npmjs.com/package/gulp-uglify">UglifyJS</a> — a JavaScript compressor/minifier</li><li><a href="http://postcss.org/">Postcss</a> — A tool for transforming CSS with JavaScript</li><li><a href="https://github.com/jakubpawlowicz/clean-css">cleanCSS</a> — Fast and efficient CSS optimizer for node.js and the Web</li><li><a href="https://github.com/senchalabs/cssbeautify">CSS Beautify</a> — a JavaScript implementation of reindenter and reformatter for styles written in CSS</li><li><a href="https://github.com/postcss/autoprefixer">Autoprefixer</a> — A plugin to parse CSS and add vendor prefixes to CSS rules</li><li><a href="https://www.browsersync.io/">Browsersync</a> — Time-saving synchronised browser testing</li></ul><h4>Add package.json file</h4><p>Before installing the tools, we need to create the package.json file. It is the configuration file for <a href="https://npmjs.com/">NPM</a> which contains the details of the project and its dependencies.</p><p>It is very easy to create, open the command prompt, go to your project directory and run the command…</p><pre>npm init</pre><p>and when asked, type in the details of your project like name, description, version etc. It will create the package.json file on the root of the project directory for you. More details and option of the file can be found <a href="https://docs.npmjs.com/files/package.json">here</a>.</p><h4>Install development dependencies</h4><p>Next we can install the tools, open the command prompt, go to your project directory and run the following command.</p><pre>npm install --save-dev autoprefixer del gulp gulp-babel gulp-clean-css gulp-cssbeautify gulp-jshint gulp-postcss gulp-rename gulp-uglify jshint browser-sync node-sass uglify-save-license</pre><p>The --save-dev parameter tells the npm to install them as the development dependency.</p><h4>Install project dependencies</h4><p>Since we are developing the jQuery plugin, we can also install jQuery from npm. Note that we are using -save, to tell npm that it is a project dependency.</p><pre>npm install -save jquery</pre><p>These commands will install all the dependencies on the node_modules directory of your project.</p><h3>3. Create Gulp tasks</h3><p>Now we can create Gulp tasks, we create 3 kinds of Gulp tasks for the project.</p><ol><li><strong>clean</strong> =&gt; Delete all files in the dist folder to keep the directory fresh for every build.</li><li><strong>lint</strong> =&gt; Lint the JavaScript files for errors and potential problems.</li><li><strong>build</strong> =&gt; Build the JavaScript/CSS files on dist folder.</li></ol><p>To create the task, we need to create a configuration file for Gulp called gulpfile.js. It is a simple JavaScript file for defining the tasks. You can create the file on the root of your project directory and let&#39;s see what will be the contents of it. Also, you can see the full gulpfile.js file on GitHub.</p><p><a href="https://github.com/techlab/jquery-gulp-example/blob/master/gulpfile.js">See the full example file on GitHub</a></p><p>First, we can include the plugins used by the tasks.</p><pre>// Include the required tools used on tasks <br>var gulp          = require(&#39;gulp&#39;),<br>    jshint        = require(&#39;gulp-jshint&#39;),<br>    rename        = require(&#39;gulp-rename&#39;),<br>    uglify        = require(&#39;gulp-uglify&#39;),<br>    saveLicense   = require(&#39;uglify-save-license&#39;),<br>    babel         = require(&quot;gulp-babel&quot;),<br>    postcss       = require(&#39;gulp-postcss&#39;),<br>    cleanCSS      = require(&#39;gulp-clean-css&#39;),<br>    cssbeautify   = require(&#39;gulp-cssbeautify&#39;),<br>    autoprefixer  = require(&#39;autoprefixer&#39;),<br>    sass          = require(&#39;gulp-sass&#39;),<br>    del           = require(&#39;del&#39;);<br></pre><pre>// Initialise plugins<br>sass.compiler     = require(&#39;node-sass&#39;);<br>var Server        = require(&#39;karma&#39;).Server;<br>var browserSync   = require(&#39;browser-sync&#39;).create();<br>var reload        = browserSync.reload;</pre><p>Then we can set our source and destination files and directories on a variable, So that to avoid repeating it.</p><pre>// Specify the Source files<br>var SRC_JS        = &#39;src/js/*.js&#39;;<br>var SRC_CSS       = &#39;src/css/*.css&#39;;<br>var SRC_SCSS      = &#39;src/scss/*.scss&#39;;</pre><pre>// Specify the Destination folders<br>var DEST_JS       = &#39;dist/js&#39;;<br>var DEST_CSS      = &#39;dist/css&#39;;<br>var DEST_SCSS     = &#39;src/css&#39;;</pre><h4>Clean Tasks</h4><p>We have two tasks for clean, clean:js and clean:css. It just deletes the dist/js and dist/css directories.</p><pre>// CLEAN files <br>gulp.task(&#39;clean:js&#39;, function () { <br>    return del([DEST_JS]); <br>}); </pre><pre>gulp.task(&#39;clean:css&#39;, function () { <br>    return del([DEST_CSS]); <br>});</pre><p>You can call the tasks by running</p><pre>gulp clean:js</pre><pre>gulp clean:css</pre><h4>Lint Task</h4><p>The lint:js task will call JSHint on each JavaScript files and report the potential errors and problems.</p><pre>// Lint JS <br>gulp.task(&#39;lint:js&#39;, function() { <br>    return gulp.src(SRC_JS) <br>               .pipe(jshint()) <br>               .pipe(jshint.reporter(&#39;default&#39;)) <br>               .pipe(jshint.reporter(&#39;fail&#39;)); <br>});</pre><p>You can call the tasks by running</p><pre>gulp lint:js</pre><h4>Build Tasks</h4><p>We have 2 build tasks here, one for JavaScript files and another one for CSS files.</p><p>The build:js task is configured to call clean:js and lint:js tasks before running build. So that it will clean the directory and check for errors before doing the build. The task will fail if we have errors while linting and avoid creating error files on dist directory.</p><p>The build:js task will read all the JavaScript files one by one and call Babel for transpile the files and save them on dist/js directory. Then it uses UglifyJS to minimize and compress the JavaScript files, adds .min suffix on filename and then saves files on dist/js directory.</p><p>The build:css task first uses <a href="https://github.com/postcss/autoprefixer">Autoprefixer</a> to add prefix to our css rules. For example we only have to write border-radius: 5px; in our source CSS and the Autoprefixer will add the missing prefixes like -webkit-border-radius: 5px;, -mos-border-radius: 5px; for browser compatibility. Then the task uses the <a href="https://github.com/senchalabs/cssbeautify">CSS Beautify</a> plugin to convert our css in a well formatted style and save it on dist/css. It will give a nicer look to our non minified css files. And finally, use <a href="https://github.com/jakubpawlowicz/clean-css">CleanCSS</a> to minify and compress the css files, adds .min suffix on filename and then save files on dist/js directory.</p><pre>// Build JS <br>gulp.task(&#39;build:js&#39;, [&#39;clean:js&#39;, &#39;lint:js&#39;], function() { <br>    return gulp.src(SRC_JS) <br>               .pipe(babel()) <br>               .pipe(gulp.dest(DEST_JS))      <br>               .pipe(uglify({preserveComments:&#39;license&#39;})) <br>               .pipe(rename({suffix: &#39;.min&#39;})) <br>               .pipe(gulp.dest(DEST_JS)); <br>}); </pre><pre>// Build CSS <br>gulp.task(&#39;build:css&#39;, [&#39;clean:css&#39;], function () { <br>    return gulp.src(SRC_CSS)<br>               .pipe(postcss( [autoprefixer({browsers: [&#39;last 10 versions&#39;]})] ))               <br>               .pipe(cssbeautify({ autosemicolon: true })) <br>               .pipe(gulp.dest(DEST_CSS))             <br>               .pipe(cleanCSS({compatibility: &#39;ie8&#39;}))<br>               .pipe(rename({suffix: &#39;.min&#39;}))  <br>               .pipe(gulp.dest(DEST_CSS)); <br>});</pre><p>You can call the tasks by running</p><pre>gulp build:js</pre><pre>gulp build:css</pre><p>As as result of all these tasks, the dist directory will contain two directories css and js. Each of that directory will contain error free and beautiful JavaScript and CSS files along with its minified and compressed versions.</p><h4>Default Task</h4><p>Finally, we can specify the default task for Gulp, this will make all the above task run automatically when we simply call gulp from the command line.</p><pre>// DEFAULT task <br>gulp.task(&#39;default&#39;, function() { <br>   gulp.start( &#39;build:js&#39;, &#39;build:css&#39; ); <br>});</pre><p>You can call it by running</p><pre>gulp</pre><h3>4. Write your jQuery plugin</h3><p>We are done with our development setup, now we have to only worry about our plugin code. Use src/js directory to write your JavaScript source code and src/css directory for CSS files. Also, the demo HTML pages can be saved on example directory.</p><p>The example project has a very simple jQuery plugin called “alterMe”. The plugin alters the text inside an element to uppercase or lowercase. I am not including the plugin code here, but you can see the plugin files on GitHub.</p><p><a href="https://github.com/techlab/jquery-gulp-example">See the example project on GitHub</a></p><h3>5. Build the project</h3><p>Every time you do changes on your project, just run gulp on your project directory from the command line and sit back. Gulp will do all the clean, lint and build task for you, just remember the magic word...</p><pre>gulp</pre><h3>Extend Gulp Tasks</h3><p>We have setup the basic development tasks, we can still extend to add more tasks and features. Let’s discuss some more useful tasks we can add to our project.</p><h3>Watch Files</h3><p>Instead of running the tasks every time, you can further automate it by making the tasks run when you modify your files.</p><pre>// WATCH for file changes and run the tasks <br>gulp.task(&#39;watch&#39;, function() { <br>    gulp.watch(SRC_JS, [&#39;build:js&#39;]); <br>    gulp.watch(SRC_CSS, [&#39;build:css&#39;]); <br>});</pre><p>Now run this command and start writing code</p><pre>gulp watch</pre><h3>Unit Testing</h3><p>There are many other libraries avaliable for JavaScript unit testing. See this StackOverflow discussion to know more about it.</p><p><a href="http://stackoverflow.com/questions/300855/javascript-unit-test-tools-for-tdd">JavaScript unit test tools for TDD</a></p><h3>JavaScript Extensions</h3><p>Instead of just writing code in plain JavaScript, you can use any of the advanced JavaScript Extensions like <a href="http://coffeescript.org/">CoffeeScript</a>, <a href="https://www.typescriptlang.org/">TypeScript</a>, <a href="http://es6-features.org/">ECMAScript</a>, etc. and use Gulp tasks to compile that to JavaScript. Please refer the gulp plugins <a href="https://www.npmjs.com/package/gulp-coffee">gulp-coffee</a>, <a href="https://www.npmjs.com/package/gulp-babel">gulp-babel</a>, <a href="https://www.npmjs.com/package/gulp-typescript">gulp-typescript</a>.</p><p><a href="https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js">List of languages that compile to JS</a></p><h3>CSS Extensions</h3><p>You can use CSS Extensions like <a href="http://sass-lang.com/">Sass</a> or <a href="http://lesscss.org/">Less</a> instead of writing plain CSS and use Gulp tasks to convert to CSS. Please refer the gulp plugins <a href="https://www.npmjs.com/package/gulp-sass">gulp-sass</a>, <a href="https://www.npmjs.com/package/gulp-less">gulp-less</a></p><h3>CSS Lint</h3><p>Like we do JavaScript linting in this example, we can also add CSS linting. Please refer the gulp plugins <a href="https://www.npmjs.com/package/gulp-csslint">gulp-csslint</a>, <a href="https://www.npmjs.com/package/gulp-stylelint">gulp-stylelint</a> also the good article about style linting</p><p><a href="http://www.creativenightly.com/2016/02/How-to-lint-your-css-with-stylelint/">How to lint your Sass/CSS properly with Stylelint</a></p><h3>HTML Minification</h3><p>You can minify your HTML using <a href="https://github.com/jonschlinkert/gulp-htmlmin">gulp-htmlmin</a>.</p><pre>npm install gulp-htmlmin --save-dev </pre><p>Task configuration</p><pre>var gulp = require(&#39;gulp&#39;); <br>var htmlmin = require(&#39;gulp-htmlmin&#39;); </pre><pre>gulp.task(&#39;minify&#39;, function() { <br>    return gulp.src(&#39;src/*.html&#39;) <br>               .pipe(htmlmin({collapseWhitespace: true})) <br>               .pipe(gulp.dest(&#39;dist&#39;)); <br>});</pre><h3>Bundling Files</h3><p>You can bundle your files to a single file using <a href="https://www.npmjs.com/package/gulp-concat">gulp-concat</a>.</p><pre>npm install gulp-concat --save-dev</pre><p>Task configuration</p><pre>var gulp = require(&#39;gulp&#39;); <br>var htmlmin = require(&#39;gulp-concat&#39;); </pre><pre>// Bundle JS <br>gulp.task(&#39;bundle:js&#39;, function () { <br>    gulp.src(SRC_JS) // path to your files <br>        .pipe(concat(&#39;bundle.js&#39;)) // concat and name it &quot;bundle.js&quot; <br>        .pipe(gulp.dest(DEST_JS)); <br>}); </pre><pre>// Bundle CSS <br>gulp.task(&#39;bundle:css&#39;, function () { <br>    gulp.src(SRC_CSS) // path to your files <br>        .pipe(concat(&#39;bundle.css&#39;)) // concat and name it &quot;bundle.css&quot; <br>        .pipe(gulp.dest(DEST_CSS)); <br>});</pre><h3>Git Actions</h3><p>You can perform Git actions like commit and push with <a href="https://www.npmjs.com/package/gulp-git">gulp-git</a>.</p><h3>Further Read</h3><p><a href="https://julienrenaux.fr/2014/05/25/introduction-to-gulp-js-with-practical-examples/">Introduction to Gulp.js with practical examples</a></p><p><a href="https://ericlbarnes.com/2014/08/22/building-jquery-plugin-gulp/">Building a jQuery Plugin with Gulp</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae5d2c180f85" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PHP Programming Best Practices and Coding Styles]]></title>
            <link>https://medium.com/techlaboratory/php-programming-best-practices-and-coding-styles-e43234446fd3?source=rss-311233931c99------2</link>
            <guid isPermaLink="false">https://medium.com/p/e43234446fd3</guid>
            <category><![CDATA[coding-style]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[best-practices]]></category>
            <dc:creator><![CDATA[Dipu Raj]]></dc:creator>
            <pubDate>Thu, 11 May 2017 06:54:51 GMT</pubDate>
            <atom:updated>2020-04-24T03:57:55.663Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>Best Practices and Coding Styles to learn to become a better PHP Programmer</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/0*U6GR_dRvVCd3fX7n.png" /></figure><p>We are all good programmers and we know how to write good programs, but there are some hidden things we might have missed in the programming world. Knowing it could help us to improve our code in speed and readability. I would like to point out few tips and practices in PHP that I found useful.</p><p>As you know, PHP (Hypertext Preprocessor) is a widely-used, free, efficient, and versatile programming language that allows developers to create dynamic content, basically used for developing web based applications. Let’s dig a little bit deep into it to see how we can improve the way we write it. This article is meant to help PHP beginners and above, If you haven’t learned the fundamentals of PHP programming yet, take your time to learn and come back.</p><h3>1. Avoid short PHP tags</h3><p>Short tags (&lt;? ?&gt;, &lt;?=?&gt;) are easy to use, but it is not always reliable, because short tags can be disabled using the short_open_tag ini setting and not all servers are enabled it by default. So the code written in short tags is not portable across servers unless you have access to the ini settings. Another reason to avoid short tag is it may confuse with &lt;?xml ?&gt; notation. The recommended use of the PHP tag is &lt;?php</p><pre>&lt;?php <br>    // My super portable code goes here<br>?&gt;</pre><p>Plus, if you are sure that your PHP version is 5.4.0 and above, you are allowed to use &lt;?= ?&gt; tag too. It is a shortcut syntax of echo and it is pretty handy to print a variable in the inline code. Since PHP 5.4.0, this tag is made free from the short_open_tag ini configuration setting.</p><pre>&lt;?=$myVariableToPrint?&gt;</pre><p>Now, push yourself hard to avoid using this tag</p><pre>&lt;?<br>    // My code that may not work on all servers<br>?&gt;</pre><p>Well, in the history there are other tags are also used that are abandoned in the latest versions(PHP7+). Just listing them to add it your DONT’S list.</p><pre>&lt;% %&gt; &lt;%=%&gt; // ASP style tag support in PHP<br>&lt;script language=&quot;php&quot;&gt; &lt;/script&gt;</pre><h3>2. Avoid closing tag in PHP only files</h3><p>If your file contains only PHP code, the closing tag (?&gt;) is optional. It is recommended to avoid closing tag because it prevents unwanted white-space or new lines being added at the end of files. That is handy to use output buffering and we will be able to add headers to the response later.</p><p><a href="http://php.net/manual/en/language.basic-syntax.phptags.php">PHP: PHP tags - Manual</a></p><h3>3. Code Formatting and Layout</h3><p>It doesn’t matter in-terms of code execution if we didn’t format the code well. But it does matter, when another person came to read it or another team try to update it or even worst when we come back to review our own code after a long time.</p><p>Well formatted code is easier to understand and it look consistent, clean and readable. It didn’t take too much effort, you can use the automatic code formatting feature of your IDE — most of the good IDE have that option, or you take a little time to format it yourself. Read more about PSR-2: Coding Style Guide which is a coding style standard for code formatting.</p><p><a href="http://www.php-fig.org/psr/psr-2/">PSR-2: Coding Style Guide - PHP-FIG</a></p><p>See an example of well indented code:</p><pre>function calculate($a, $b, $c) {<br>    $result = 0;<br>    $x = $a + $b;<br>    if($x &gt;= $c){<br>      $result = ($a + $b) * $c;<br>    }else{<br>      $result = $a * ($b + $c);<br>    }<br>    return $result;<br>}</pre><p>And this is NOT, don’t do like this</p><pre>function calculate($a, $b, $c) { $result = 0;<br>    $x = $a + $b;<br>if($x &gt;= $c){<br>          $result = ($a + $b) * $c;  }else{<br> $result = $a * ($b + $c); }<br>    return $result;<br>}</pre><h3>4. Write Comment Code</h3><p>Comments are the part of code that helps to describe what is a piece of code or a function or a class is doing. Writing comments is a good practice to let others know what you’re doing or to remind yourself what you did. You don’t want to comment every line in your code but do when you think it is necessary to understand your code later. <br>These are the examples of commenting styles on PHP</p><pre>&lt;?php<br>echo &#39;Hello World 1&#39;; // A single line comment<br>echo &#39;Hello World 2&#39;; # Another way to do a single line comment<br><br>/*<br>   This is a multi-line comment<br>   Its cool when I want to write an essay about my code <br>*/<br>echo &#39;Hello World 3&#39;;</pre><p><a href="http://php.net/manual/en/language.basic-syntax.comments.php">PHP: Comments - Manual</a></p><h3>5. Choosing Names</h3><p>Naming your variables, functions, or classes are important. While naming, you should consider these factors</p><ol><li>The name you chose should be meaningful</li><li>The name should be in a language everybody will understand (preferably in English)</li><li>Follow consistent naming convention</li></ol><p>There are many naming standards with slight variations, like PSR-1: Basic Coding Standard or Zend Framework Naming Conventions. You can choose any style based on your convenience, just make sure it is consistent throughout the project. <br> Mostly I use the following naming convention style</p><ul><li>ClassName</li><li>function_name</li><li>$variable_name</li><li>CONSTANT_NAME</li></ul><p><em>Here are the list of best read about naming conventions</em></p><ul><li><a href="http://www.php-fig.org/psr/psr-1">PSR-1: Basic Coding Standard - PHP-FIG</a></li><li><a href="http://framework.zend.com/manual/1.12/en/coding-standard.naming-conventions.html">Manual - Documentation - Zend Framework</a></li><li><a href="http://programmers.stackexchange.com/questions/149303/naming-classes-methods-functions-and-variables">Naming classes, methods, functions and variables</a></li></ul><h3>6. Understanding ‘echo’</h3><p>The PHP echo statement is a language construct (not exactly a function) used to output data to the screen. echo and print both do the same thing, but echo is a little faster. echo can take multiple parameters, and it is faster than concatenating strings.</p><p>For example...</p><pre>echo &quot;Hello &quot;, $name, &quot;!&quot;;</pre><p>is faster and neater than…</p><pre>echo &#39;Hello &#39; . $name . &#39;!&#39;;</pre><p>Also you can use expressions</p><pre>echo &quot;Sum: &quot;, 1 + 2;</pre><p>which is better than…</p><pre>echo &#39;Sum: &#39; . (1 + 2);</pre><p>But echo has no return value, so it cannot be used in expressions. So the following code is invalid!</p><pre>($some_var) ? echo &#39;true&#39; : echo &#39;false&#39;;</pre><p>instead you can use like this</p><pre>echo $some_var ? &#39;true&#39; : &#39;false&#39;;</pre><p><a href="http://php.net/manual/en/function.echo.php">PHP: echo - Manual</a></p><h3>7. Using variables</h3><p>It is not necessary to initialize variables in PHP however it is a good practice. Uninitialized variables will have a default value, relying on the default value may end up in unexpected results of your program. So it is recommended to initialize your variables before using them, and that will make your code much cleaner.</p><pre>$my_array = array();<br>$my_integer = 0;<br>$my_boolean = false;</pre><p>Also avoid using unwanted variable declarations. Variables cost memory, so removing unwanted variable declaration can reduce memory load. For example…</p><pre>$my_name = $data[&#39;name&#39;]; <br>echo $my_name;</pre><p>In the above code we can definitely avoid the variable $my_name, like...</p><pre>echo $data[&#39;name&#39;];</pre><h3>8. Use of Ternary Operators</h3><p>A simple if/else statement can be changed to make use of ternary operators. It makes your code shorter, readable and saves few lines too. The usage is simple…</p><pre>// Usage of Ternary Operators <br>$variable = (CONDITION) ? IFTRUE : IFFALSE; <br><br>// Similar if/else condition usage in one line<br>if(CONDITION) { $variable = IFTRUE; } else { $variable = IFFALSE; }</pre><p>An example usage of ternary operators</p><pre>$action = (empty($_POST[&#39;action&#39;])) ? &#39;default&#39; : $_POST[&#39;action&#39;];</pre><p>Is identical to this if/else statement</p><pre>if (empty($_POST[&#39;action&#39;])) {<br>    $action = &#39;default&#39;;<br>} else {<br>    $action = $_POST[&#39;action&#39;];<br>}</pre><p><a href="http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary">PHP: Comparison Operators - Manual</a></p><h3>9. Use of Comparison Operators</h3><p>PHP have strict and loose comparison operators, the difference is strict comparison validates value and type while loose comparison only validates value. You can use any of the comparison, but to avoid unexpected program behavior, you should be very clear what you are doing.</p><p>It is not a big deal to choose, if you want to compare the value and the type go for strict comparison otherwise use loose ones.</p><p>For example, you are validating a POST value and you don’t know in which type it will come always and you only care about the value. Then go for loose comparison</p><pre>// Pass if $_POST[&#39;user_id&#39;] is an integer 1234 or a string &#39;1234&#39;</pre><pre>if($_POST[&#39;user_id&#39;] == &#39;1234&#39;){  <br>    echo &#39;Hello Special user&#39;;<br>}</pre><p>But, for example, you are validating a return value from a function and you really care the type of the return value. In the below example if you don’t use strict comparison the condition will pass if the function return ‘1’, which will be a serious flaw.</p><pre>// Pass only if the function IsThisAdmin() return a boolean true</pre><pre>if(IsThisAdmin($_POST[&#39;user_id&#39;]) === true){<br>    echo &#39;Hello Admin&#39;;<br>}</pre><p>Another very common example for this is while using strpos to compare strings. Here in the below example, if you use != for comparison, it print output as &#39;string not found&#39;, which is wrong.</p><p>The reason is strpos return 0, which is the position of the string and the loose comparison consider 0 as false, hence the unexpected result. So the correct check is as follows...</p><pre>if (strpos(&#39;abcdef&#39;, &#39;a&#39;) !== false) {<br>    echo &#39;string found&#39;; // Found, thanks to operator !==<br>} else {<br>    echo &#39;string not found&#39;; // Not found, thanks to operator !==<br>}</pre><ul><li><a href="http://php.net/manual/en/language.operators.comparison.php">PHP: Comparison Operators - Manual</a></li><li><a href="http://php.net/manual/en/function.strpos.php">PHP: strpos - Manual</a></li></ul><h3>10. About Strings</h3><p>Let us take a look at common ways of specifying strings and its use in PHP.</p><h4>Single quoted</h4><p>Enclosed in single quotes (&#39;) is a simple way to specify a string. It doesn&#39;t parse any special characters, but it allow escape character with a backslash (\), generally for escaping a single quotes and backslash itself in the string. So this way is ideal if you want to output a special character literally, such as \r or \n, and this method is good if you have a simple string to specify.</p><pre>echo &#39;Hello, \n this is a newline&#39;; <br>// Outputs: Hello, \n this is a newline</pre><h4>Double quoted</h4><p>Enclosed in double-quotes (&quot;) is a another way to specify a string, and PHP will interpret more escape sequences for special characters. So \n in the string really output a newline instead of literally printing it. And variables specified in the string will be parsed and replaced with its value, if available. This method is ideal if you have to specify variables or special characters in your string.</p><pre>$name = &quot;Dipu Raj&quot;;<br><br>echo &quot;Hello, My name is $name&quot;; <br>// Outputs: Hello, My name is Dipu Raj<br><br>echo &quot;See the first character of my name &#39;{$name[0]}&#39;&quot;;  <br>// Outputs: See the first character of my name &#39;D&#39;</pre><p>There are many talk about performance of both string specifications. Some point out that single quoted is faster because it doesn’t include much parsing. But in my opinion both give same performance on normal use, a few parsing doesn’t make noticeable performance lack. So use any method you want as the situation demands, unless you want to do micro-optimization.</p><p><strong>Heredoc</strong> and <strong>Nowdoc</strong> are the other complex way of specifying strings, they are not used very common. You can read more about them on PHP strings manual.</p><p><a href="http://php.net/manual/en/language.types.string.php">PHP: Strings - Manual</a></p><h3>11. About Arrays</h3><p>Array in PHP is optimized for several different uses, it can be used as an array, list, hash table, dictionary, collection, stack, queue, etc. Let’s look into some cool features of PHP Array probably don’t use commonly.</p><p>Since PHP 5.4, we get the short array syntax, which allow to use [] instead of array(). Checkout the short and regular syntax example...</p><pre>// Short array declaration syntax, as of PHP 5.4<br>$my_array = [1, 2, 3, 4]; <br>$my_array_2 = [&#39;one&#39; =&gt; 1, &#39;two&#39; =&gt; 2, &#39;three&#39; =&gt; 3, &#39;four&#39; =&gt; 4];<br> <br>// Regular array declaration syntax, all PHP versions<br>$my_array = array(1, 2, 3, 4); <br>$my_array_2 = array(&#39;one&#39; =&gt; 1, &#39;two&#39; =&gt; 2, &#39;three&#39; =&gt; 3, &#39;four&#39; =&gt; 4);</pre><p>Square brackets and curly braces can be used for accessing array elements. Both line of code have same effect in the below example…</p><pre>echo $my_array[2];<br>echo $my_array{2};</pre><p>Since PHP 5.4, we can directly access the result of a function or method as array (array dereferencing).</p><pre>&lt;?php<br>function getColors() {<br>    return array(&#39;red&#39;, &#39;blue&#39;, &#39;green&#39;, &#39;yellow&#39;);<br>}<br><br>// As of PHP 5.4<br>$second_color = getColors()[1]; <br><br>// Previously it was only possible using a temporary variable<br>$tmp = getColors();<br>$second_color = $tmp[1];</pre><p>The foreach loop provides an easy way to traverse an array</p><pre>foreach ($my_array as $key =&gt; $value)<br>{<br>    echo &#39;Key: &#39;, $key, &#39; Value: &#39;, $value;<br>}</pre><p>Since PHP 5, we can manipulate array value directly by passing them by reference.</p><pre>// PHP 5<br>foreach ($colors as &amp;$color) {   // &amp;$color pass by reference<br>    $color = strtoupper($color);<br>}<br><br>/* Make sure to unset the reference variable, <br>   it may affect the array value if the variable $color is modified elsewhere in the code */<br>unset($color); // Unset the reference variable<br><br>// Older versions workaround<br>foreach ($colors as $key =&gt; $color) {<br>    $colors[$key] = strtoupper($color);<br>}</pre><p>Use quotes if you have array key as a string. It still works if you don’t, but that is not recommended. The reason is that PHP assume it as a constant if you don’t enclose in quotes, and PHP automatically converts it into a string if there is no defined constant in that name, that’s why it still works, but we are giving the compiler an extra load. Also if you already have a constant with the same name, you get weird results.</p><pre>echo $my_array_2[&#39;three&#39;]; // Correct</pre><pre>echo $my_array_2[three]; // Wrong code</pre><ul><li><a href="http://php.net/manual/en/language.types.array.php">PHP: Arrays - Manual</a></li><li><a href="http://php.net/manual/en/function.array.php">PHP: array - Manual</a></li></ul><h3>Conclusion</h3><p>I hope that the tips and practices discussed here was helpful to you. However, programming is vast, don’t stop learning, try new things and be up to date. If you think there is an incorrect information or missing clarification, please let know with your comment.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e43234446fd3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techlaboratory/php-programming-best-practices-and-coding-styles-e43234446fd3">PHP Programming Best Practices and Coding Styles</a> was originally published in <a href="https://medium.com/techlaboratory">TechLaboratory</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[8 Best jQuery step wizard plugins]]></title>
            <link>https://medium.com/@dipuraj/8-best-jquery-step-wizard-plugins-1611612edcb1?source=rss-311233931c99------2</link>
            <guid isPermaLink="false">https://medium.com/p/1611612edcb1</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[wizards]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jquery]]></category>
            <dc:creator><![CDATA[Dipu Raj]]></dc:creator>
            <pubDate>Wed, 10 May 2017 11:12:41 GMT</pubDate>
            <atom:updated>2020-06-23T14:30:01.825Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>Handpicked collection of 8 best jQuery Wizard plugins</h4><p>Wizards are a nice user interface design to lead the users to perform a task in a step by step order like a shopping checkout or user registration screen. Wizards can make a complicated screen divided into simple, small and grouped parts and so effectively simplify the task for the users.</p><p>Here we are discussing the best jQuery wizard plugins that can make a nice wizard interface.</p><h3>1. jQuery Smart Wizard</h3><p>The awesome jQuery step wizard plugin with Bootstrap support</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/0*HGj5rtBCE-PAceuO.png" /></figure><p><a href="http://techlaboratory.net/jquery-smartwizard">View</a> · <a href="https://github.com/techlab/SmartWizard/archive/master.zip">Download</a> · <a href="http://techlaboratory.net/jquery-smartwizard#demo">Demo</a></p><h3>2. jQuery Wizard</h3><p>A powerful jQuery plugin for creating step-by-step wizards with bootstrap</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/755/0*iLwY0ayc0VhA0dtd.png" /></figure><p><a href="https://github.com/thecreation/jquery-wizard">View</a> · <a href="https://github.com/amazingSurge/jquery-wizard/archive/master.zip">Download</a> · <a href="https://github.com/amazingSurge/jquery-wizard#examples">Demo</a></p><h3>3. Multi-Step Form</h3><p>Multi Step Form with jQuery validation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/0*nj4-5Ih483q6rZQh.png" /></figure><p><a href="https://github.com/mgildea/Multi-Step-Form-Js/">View</a> · <a href="https://github.com/mgildea/Multi-Step-Form-Js/archive/master.zip">Download</a> · <a href="http://jsfiddle.net/mgildea/ez94n125/13/show/">Demo</a></p><h3>4. jQuery Steps</h3><p>A powerful jQuery wizard plugin that supports accessibility and HTML5</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/0*cexmeXpXlprai4Hj.png" /></figure><p><a href="http://www.jquery-steps.com/">View</a> · <a href="https://github.com/rstaib/jquery-steps/archive/master.zip">Download</a> · <a href="http://www.jquery-steps.com/Examples">Demo</a></p><h3>5. jquery-steps</h3><p>Lightweight jQuery step wizard plugin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/0*0TrEQcp2XQNLAyX_.png" /></figure><p><a href="https://oguzhanoya.github.io/jquery-steps/">View</a> · <a href="https://github.com/oguzhanoya/jquery-steps/archive/master.zip">Download</a> · <a href="https://oguzhanoya.github.io/jquery-steps/#intro">Demo</a></p><h3>6. jWizard</h3><p>jQuery Plugin for generating a Windows Wizard-like interface for your Web Applications</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/0*83hIALyxLcqdwput.png" /></figure><p><a href="http://www.dbarnes.info/jWizard/">View</a> · <a href="https://github.com/dominicbarnes/jWizard/archive/master.zip">Download</a> · <a href="http://www.dbarnes.info/jWizard/#demo">Demo</a></p><h3>7. Twitter Bootstrap Wizard</h3><p>Builds a wizard out of a formatter tabbable structure</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/0*3BTk5x8aa6ltWOr8.png" /></figure><p><a href="http://vinceg.github.io/twitter-bootstrap-wizard/">View</a> · <a href="https://github.com/VinceG/twitter-bootstrap-wizard/archive/master.zip">Download</a> · <a href="http://vinceg.github.io/twitter-bootstrap-wizard/examples/basic.html">Demo</a></p><h3>8. jQuery.wizard</h3><p>An asynchronous form wizard that supports branching.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/0*OQembXXZErLGOnvC.png" /></figure><p><a href="http://kflorence.github.io/jquery-wizard/">View</a> · <a href="https://github.com/kflorence/jquery-wizard/archive/master.zip">Download</a> · <a href="http://kflorence.github.io/jquery-wizard/examples/">Demo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1611612edcb1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>