Tag Archives: reddit

Hello! Today, I would love to inform you that my book is available for download on Amazon Kindle Store for a limited time. In that order therefore, all book lovers have an opportunity to download this awesome story without paying the regular price.

That being said, you can download a copy for yourself by clicking the image above or following the link below:

After thinking hard enough, I decided to go with My Life Sentences – a True Story. I chose that title because I felt like it reflected what I have gone through as a person except in sentences.

2)What genre is your book?

My Life Sentences is based on a true story(non-fiction). Everything you read in this book happened and in fact some of them are still happening right now as we speak. I wrote this book from a personal experience and today I am giving you the opportunity to look deep into my crazy life as well as my own plan for the future.

3)Where can the book be purchased or downloaded for free?

There are several places where you can download my book today or any other day. Here they are depending on where you are located:

The above list covers most regions accessible to Amazon and anyone should be able to download a copy through the following methods:

Kindle Tablet – If you own a kindle, then you should be good to go. Just search for my book “My Life Sentences – a true story” and download it to your tablet.

Kindle App – You don’t need a tablet to read my book; if you have any of the following:

Android Smartphone – just download a Kindle App and then download the book.

iPhone – You can also download a free kindle app from the store and off you go.

Any other tablet that accepts apps – Download a free kindle app and start downloading books. If you have questions on this, please let me know.

I don’t have a kindle tablet, any other tablet or smartphone – don’t worry, you can download a desktop application from amazon (free) and then download the book after that which will be accessible through that application – Download the reading application here

I don’t have any of those tools – You can get the print version of my book here

NOTE: If you don’t have any of the above choices, please contact me and I will give you a free $10 amazon gift card to buy the print version. This will only be one reader. So, the first person to contact me because they don’t have any smartphone or tablets or PC will get $10 to buy a print version.

Thank you so much and now you can grab a copy of this book before time runs out.

If you have any questions, please let me know either through Twitter (@eenvincible) or Facebook (ElishaTheAuthor) or through the contact form on this blog. Please share this post with your friends if you liked it. That will mean the world to me.

If you also have questions on self-publishing, let me know – I can help! Thank you!

Hello! Nice to see you and thanks for stopping by. In the last two days I took a detour from programming but today I am going to pick up from where I left Google Maps APIs – Part V . Today’s post will be relatively shorter than previous ones because I am completing a section (at the end, I will give an app idea). Let us get to it. We should start with our previous finishing code:

Every time you are expecting dynamic data to be added to your map (markers), you want to make sure that none of them appears outside the map. The best way to handle this is by making a map that automatically adjusts to the markers added. In order to achieve that, we use LatLngBounds object.

LatLngBounds Object

A bounding box is simply a rectangle defining an area. Its corners consist of geographical coordinates and everything inside it is within its bounds. You can use it to calculate the viewport of a map and also determine if an object is in a certain area of the map.

The bounding box is of type google.maps.LatLngBounds object. It takes two optional arguments (southwest and northeast corners of the rectangle). Those arguments are of type google.maps.LatLng.

In order to manually create a LatLngBounds box, we have to determine the coordinates of its corners(you will see a better solution soon). Adding to our previous code, just below the line where we created our map, we could do this:

Now our three markers will be within the above coordinates: Here is what am trying to allude to.

Using The APIs For The Heavy Work

In order to extend our example to automatically adjust the viewport to fit the markers, we have established that we need a LatLngBounds object. So, we first create an empty LatLngBounds object somewhere outside our for loop.

That is it for the above case. We now have a map that fits all the markers perfectly inside the viewport. In fact, you can now add more cities and the map will automatically adjust the viewport. Oh wait, we can do this right now by adding my favorite city: Nairobi, Kenya and let us also represent Asia (seoul, South Korea – let there be peace in the peninsula).

That should do it and now we can take a look at our awesome map with more markers.

There you have it! I hope you had some fun playing around with this example. Within a very short time and few lines of code, we have created a map that shows different cities displayed using markers. Now consider having users enter their cities as you watch the markers increase. I like this so much am starting to think of a cool app.

Let me show you the entire code then give you a cooler idea of an app!

There you go. You can do whatever you want with it. Now here is an app idea (warning: it might sound silly).

Happy PlacesThe idea is simple. Use Twitter Search and Streaming APIs to track the use of a smiley face ):. It is not hard if you try. The fun part: using the user_ids of those who used the smiley face, fetch their locations (if they have set their location on their profiles) and check them against a local list of your own. If any of them match, put a marker on a map. It should be evident that more markers within a small city might indicate that residents there are friendly or happier but that is up for debate. Either way, it sounds fun. ):

See you soon. If you have questions, please ask. Take care and thanks for stopping by!

Hello! I have decided to step a side today and post something outside programming. I have always fought the urges to act robotic by reminding myself that after all is said and done, I am indeed, a human being who has questions. I laugh really hard when tickled. I cry when hurt. I bleed when pricked. Most often, my questions never get answered.

Today some people are celebrating Easter – and so “Happy Easter”. I say ‘some’ because there are those who don’t do it.

One of the biggest questions I have started asking myself is “what really happen to us when we die” and immediately after that question comes? “where do we really come from?”. Believe it or not, every time I hear people talk about the absence of a higher power, I don’t give them a dead ear – instead, I listen to what they have to say and then ask them “but how did everything happen to be?” I know some Physics and I obviously won’t start an endless discussion here on how there had to be a force that started the process of life.

What if we were not named “human beings?” I don’t really know. You might say that I could use some serious scientific reading and whatnot and I agree. However, every day I step outside my house and look around me, I see things that I can’t even explain. I see trees and rocks. I hear birds of prey singing. I am not blind to the fact that there is more to life than just what I can and cannot see.

Consider a rich guy who has worked so hard to be where he is today. The fact is that he has a right to drive the most expensive car as he pleases. He can live the best lifestyle(best here means he can buy whatever he wants). You can also look across the oceans or anywhere around the world and see poor children dying of hunger. The question is: can you really say the rich guy should take some of his money and help those poor kids? It would be a humane gesture but quite complicated. The reason is simple: you can argue for and against it. Most obviously he has a right to say no.

The other aspect of life is that perhaps if we worked a little harder, just an extra mile, we might feel satisfied with our lives. Make some extra dollars, live in a bigger house, make the best friends and have a perfect family. Most of us believe that we have to do all that in order to be happy. I don’t know about you but I would like to have a nice house and a car. Do I sound greedy? Perhaps but am not. Aren’t we advised to work really hard and earn stuff? What about those who have worked hard and yet they still cannot see success? Is it their fault? Are they lazy? Or maybe they don’t know how to define success!

I believe that most of us have focused so much on making a living, forgetting to make a life. Maybe we could have a fresh start by asking ourselves what we really want and need. We might be looking too far away instead of looking closer. Inside. The day we start to understand what we can do with what we already have, we will be happier than ever. It is not about having a lot of friends. It is about appreciating what I already have. Showing them that I care. Being human. It is not really hard. Start small.

I am going to stop here. Thank you for reading. I hope you have a good day. See you soon and you are welcome to leave a comment below this post. Always remember that it is not much to do with where we have come from, rather, it is where we are headed in this life. Stay safe and strong.

Hello! I am back and I hope you are doing good today. At the end of my fourth Google Maps APIs post, I mentioned in passing that I will be starting with Markers today and that is exactly where I will start. To make things fun, here is what a marker looks like – you have probably seen one if you have used a map before!

There you have it! So a marker is a small image that is positioned at a specific place on a map. Now we can figure out how to add it to our own map. We will start with a basic code for our map.

Adding a MarkerYou can choose to go with the default look of a marker or create your own. I will use the former in this example. A marker is of type google.maps.Marker object. This object takes one parameter which is of type google.maps.MarkerOptions . MarkerOptions has several properties that you can use to make the marker look and behave different but the basic two are: position and map.

position – this property defines the coordinates to place the marker and takes an argument of type google.maps.LatLng object.

map – the map property is a reference to the map to which you want to add the marker.

There you have it. Oh wait, we have not seen it actually on a map yet. Here it is:

Adding a tooltip

We might both agree that adding a tooltip to our marker will make it much better. A tooltip is a yellow box with some text in it that appear when you hover your mouse over a marker. To add it to our marker, we simply use title property of the MarkerOptions object.

One thing to note is that you are not limited to using the default marker icon. In fact, Google hosts a ton of other icons that you can freely use. In order to use a different icon, you provide a url location to it like this:

Normally when marking a place on a map, you might want to show more information related to that place. Google Maps APIs provides a way to do so using InfoWindow. It looks like a speech bubble and appears on top of a marker when you click it.

InfoWindow resides in the google.maps namespace. It takes one argument which is an object called InfoWindowOptions. Just like MarkerOptions object, InfoWindowOptions has several properties but the most important one is the content. This property controls what will show inside the info window. It can be plain text, HTML or a reference to HTML node.

Now that we have created our infowindow, running this code as is … well, won’t work yet. The reason is this: we have to connect the marker with our infowindow. How do we do that? Thanks to Google Maps APIs, we have google.maps.event.addListener() method to the rescue. This method takes three arguments ( the object it is attached to, the event it should listen for and the function (event handler) to call when the event is triggered.

Before I show you the code, it would be clear to mention that the InfoWindow object has a method called open() which takes two arguments – map: this is a reference to the map it will be added to(in case you have more than 1 map) and the second argument is the object that the InfoWindow will attach itself to. In our case, we want to attach it to the marker being clicked!

The question you might have asked yourself is this: how do I add more than one marker to my map without manually creating them? One thing I have assumed all along is that you are familiar with JavaScript and with that in mind, using a for loop and an array sounds like a good idea!

More United States Cities on the Map

I am going to show you the entire piece of code that does several things: (i) add markers (ii) add infowindows and (iii) eliminate duplicate windows. It will make much sense once I explain it.

Three markers:
Clicking on any of the markers results in the following:

Everything up until line 11 should be second nature to you by now, I hope. Now, at line 12…15, I simply defined an array and added three cities to it.

On line 17 – I defined a variable infowindow which will come in handy when we want to avoid duplicating info windows by checking if one already exists.

On line 18…24 – I use a for loop to create markers and from

Line 26…37 – I used an anonymous function within the for loop to add event listeners to each of the markers. Everything within the anonymous function is much like what we have been doing all along – creating an info window, setting the content and finally opening it when a marker is clicked.

I am using closures here to avoid a situation where no matter which marker you click, the infowindow will open for the marker that was created last(always ‘infowindow number 3’). You can read more about closures by visiting Douglas Crockford’s Post .

I am going to stop here because I think this is long enough. I will do more fun stuff next time. If you notice any errors, please notify me and I will fix them as soon as possible. If you have any questions please let me know through the comments section. Hopefully this taught you something meaningful. Thank you for stopping by.

Hello! Thanks for stopping by. I didn’t post anything yesterday because I was asked to read and review an AngularJS Starter book which ended up taking all my time. It was a good experience because I learned some new stuff. That being said, today, I am going to continue with our Google Maps APIs series. My last post ended with the code below and so let us pick up from there:

With the navigationControlOptions property, you determine the look of the navigationControl. It pretty much works like mapTypeControlOptions by taking an object as its value. The object is of type: google.maps.NavigationControlOptions and it has two properties : position and style. It might sound familiar if you read my first two posts.

position – this property is of type google.maps.ControlPosition.

style – this property comes in several flavors and they all reside in google.maps.NavigationControlStyle. They are :

DEFAULT – if set to this value, the control will vary according to the map size and other factors. It displays either small or large.

SMALL – this is, well, the small control. Only allows you to zoom the map.

ANDROID – android anybody? This control is specifically tailored for android smartphones.

ZOOM_PAN – this is the large control that lets you to both zoom and pan the map.

As you can see, the position is top-right and the style is zoom_pan meaning you can do both zooming and panning of the map. In order to use the above property, you must set navigationControl to true.

scaleControl

This property determines whether the scale control will be displayed or hidden. The default value is false meaning it is not displayed. If you want to show it on your map, you must set it to true.

.
.
var options = {
zoom: 8,
.
.
scaleControl: true //this line
};

scaleControlOptions – Using this property, you control how scaleControl will be displayed. It takes an object of type google.maps.ScaleControlOptions. Just like NavigationControlOptions, it has two properties: guessed them yet? Position and Style. In order to use scaleControlOptions, you must also use and set scaleControl to true. (Sounds familiar right?).

keyboardShortcuts

This property enables or disables the ability to use the keyboard to navigate the map. It is true by default but you can change it to false to disable it.

disableDoubleClickZoom

Double-clicking on a map normally zooms in but if you want to disable that property, you set disableDoubleClickZoom to true.

draggable

You can pan the map by default by simply dragging it around. If for some personal reason you would like to disable it, set draggable to false.

scrollwheel

You can use this property to decide whether you want to use your mouse to zoom in and out by simply scrolling the wheel of your mouse. It is set to true by default. You can, for some alien reason, set it to false to disable it.

streetViewControl

This property shows or hides the Street View Control (wildly known as pegman). The default value of this property is false. You can set it to true to enable it. If you set the value to true, the map will display an orange pegman right above the zoom control.

Before I finish this post for today, I would like to show you a snapshot of our final map with the above code:

I used squares and rectangles to indicate the controls and their positions. You might be curious to know what really happens when you drag the pegman around! This is what happened when I dragged it to some street in Kansas! It switched to Street View mode – Warning: you might see someone you know, seriously, you might!
I told you it was pretty clear you might actually see your friend sneaking around. The good part, ‘we are not in Kansas anymore’.

Thanks again for stopping by and I hope you learned something from this post. Next time I will jump into Controlling the Map Container – the html div element that contains our map, then some more cool and fun stuff. If you spot an error, please let me know. Please share this post if you like it. ‘See you’ soon and take care of yourself!

Hi! First of all, thank you for stopping by. It gives me strength to keep sharing what I like because I know it is all worth the effort. That being said, today, I am going to expand on what I did on Google Maps API part I by adding more features to our map. Last time, our map.js file has the following code:

disableDefaultUI – setting this property to true disables the default user interface. This further means the default zoom control and the map type chooser will not be displayed. Even if you disable the default user interface, you can still enable these controls individually. The default value is false. Example:

Before disabling the default user interface, the map looked like this:
After disabling it, you will see something like this:

As you can see, the rectangles represent the original position of our default user interfaces.

mapTypeControlOption

In a case where you decide to have mapTypeControl visible, then mapTypeControlOption controls how it will be displayed. It can look different depending on circumstances or your own decisions to place it where you want. One good thing also is that you can define what map types you would like the user to choose from. This property takes an object of type google.maps.MapTypeControlOptions as its value. It has three properties:

style

position

mapTypeIds

As you might have guessed, while using mapTypeControlOptions, you must always remember to set mapTypeControl to true.

Style

This property determines the appearance of the control. The values you can choose from reside in the google.maps.MapTypeControlStyle object. The choices are:

DEFAULT – the look will vary depending on the window size and maybe other factors. If the map is big enough, you get a horizontal bar displayed, otherwise, a drop-down menu will be shown.

HORIZONTAL_BAR – this displays the horizontal bar!

DROPDOWN_MENU – displays a drop-down list to either save space or some other reason!

As you might have noticed, the default position of this control is in the upper-right corner. You can easily define it to appear somewhere beside the upper-right corner. To do that, you will need to use the google.maps.ControlPosition class. This class has several predefined positions you can choose from: BOTTOM, BOTTOM_LEFT, BOTTOM_RIGHT, LEFT, RIGHT, TOP, TOP_LEFT, TOP_RIGHT.

The final map now has a drop-down menu with a list of two MapTypeIds: ROADMAP and SATELLITE. Let us finish this post with a look!

I am going to stop here for today because I don’t want to make this post too long. So, I want to wrap up by saying congratulations for keeping up with me. Next time I will do some work on navigationControl and more! Thanks for stopping by and if you liked this post, please drop me a line and share it online! See you soon. If you have questions, please ask me!

According to Wikipedia , Object-oriented programming(OOP) is a programming paradigm that represents concepts as objects that have data fields (attributes that describe the object) and associated procedures known as methods. An object is an instance of a class – that is, you use a class to create an object. The easiest way to understand OOP is using a real-world example.

Looking at the above image makes our work really easier here. In real life, there is something called a car. But you can’t just walk to your friend’s house and say I own a car and assume that they will automatically know what type of car you have. So, in other words, our car is a class and from that car class, we can create different kinds of cars(objects) like Toyota, Ford, Chevy etc. Oh, don’t forget the VW Bug!

To make this a little understandable, we can break the car concept into data fields and methods. Every car has some color(attribute), and most importantly they move(method). What else can you think of? A car can either be on or off. The other fact is that different cars have different other properties. Think of them!

Another good example is a cookie-cutter. You can think of a cookie-cutter as a class and the cookies themselves as objects. Using your cookie-cutter, you can create all shapes of cookies, using different recipes.

Encapsulation

One of the biggest advantage of OOP is its capability to support and encourage encapsulation – also known as data hiding. Essentially, access to the data within an object is available only via the object’s operations, known as the interface of the object.

Polymorphism

Polymorphism means different classes can have different behaviors for the same operation. Consider having a class Car and a class Bicycle. They can both have different move operations. Polymorphism is more a characteristics of behaviors than it is of objects. In PHP, only member functions of a class can be polymorphic.

Inheritance

Inheritance allows you to create a hierarchical relationship between classes using sub-classes. If you are asking whether this works like a son inheriting a million dollars from his father or not, then you are thinking right! A subclass inherits attributes and operations from the superclass. An example could work like this: we both know that a car and a bicycle have certain things in common. You could then use a class called vehicle that contain a color attribute and a move operation that all vehicles have. You can simply then have both car and bicycle classes inherit from vehicle class – how nice right?

Using inheritance might save you work if operations can be written once in a superclass rather than many times in separate subclasses. It might also allow you to more accurately model real-world relationships. If a sentence about two classes makes sense with “is a” between the classes, inheritance is probably appropriate.The sentence “a car is a vehicle” makes sense, but the sentence “a vehicle is a car” does not make sense because not all vehicles are cars.Therefore, car can inherit from vehicle.

Creating Classes, Attributes and Operations

Let us get our hands dirty here by …you guessed it, creating classes, attributes and operations in php. When creating a class, the keyword class must be used.

<?php
#the structure of a class
#opening and closing braces
#---------------------------------------------------------#
class classname
{
#do stuff here - attributes and operations
}
#---------------------------------------------------------#
#Obviously, without attributes and operations, our class
#is not that useful. So let us add some attributes
#---------------------------------------------------------#
class classname
{
public $attribute1;
public $attribute2;
}
#---------------------------------------------------------#
#the above code creates a class called classname and
#two attributes $attribute1, $attribute2 with [public]
#visibility. visibility options[public, private, protected]
#We have added attributes but we have no way of operating
#on them, so why not add some methods to our brand new class?
#---------------------------------------------------------#
class classname
{
public $attribute1;
public $attribute2;
#now add two methods here
function operation1(){
//do some cool math here
}
function operation2($param1, $param2){
//do some other crazy rocket-propulsion.
}
}
#--------------------------------------------------------#
#As you can see, we created two functions inside our class
#and we call such functions ... methods because they are in
#the class!
?>

Constructors

Most classes have a special type of operation called a constructor. A constructor is called when an object is created and also normally performs useful initialization tasks such as setting attributes to sensible starting values or creating other objects needed by the object.

A constructor is declared the same way as other operations inside a class but has the special name __construct() and although you can manually call a constructor, its purpose is to be called automatically when an object is created.Time for an example:

<?php
#declaring a constructor for a class
#----------------------------------------------#
class classname
{
public __construct($param) #our constructor
{
//do some initialization or setup here
echo "Constructor called with parameter: ".$param."<br />";
}
}
#PHP supports function overloading - providing more than one
#function with the same name with different numbers or types
#of parameters!
?>

Destructors

Just like constructors, there exist destructors. They are special in the sense that they don’t take any parameters and declared as __destruct(). When all references to a class have been unset or fallen off a scope, it gets destroyed. If you then want to have some functionality executed before the class is destroyed, you use the destructor!

Instantiating A Class

After declaring your class, you need to create an object – a particular individual that is a member of the class. You can then do something with it. As I mentioned earlier, it is called instantiating or creating an instance of a class. You create an object by using the newkeyword. When you do so, you need to specify which class your object will be an instance of and provide any parameters required by the constructor. Time for an example:

Using Class Attributes

Within a class, you have access to special pointer called $this. If an attribute of your current class is called $attribute you refer to it as $this->attribute when either setting or accessing the variable from an operation within the class.

Doing what we did above(accessing attributes and changing their values outside a class is obviously not a good idea.To avoid such a problem, we take advantage of OOP’s encapsulation. We enforce it by using __get and __set functions. These functions are commonly known as setters and getters or accessor functions. Example:

Now our above code provides minimal functions to access our attributes. Basically, our __get function returns the value of the attribute with the same name as the one passed in ($name). __set() on the other hand takes a name of an attribute and a value, then sets the value of the attribute to the value passed in!

One thing to remember is that you do not call __get and __set functions directly. You may be asking, so how do we then call them? Here is the thing: when you instantiate an object like this:

<?php
$a = new classname();
?>

You can then use __get() and __set() functions to check and set the value of any attributes: If you type:

<?php
$a->$attribute = 5;
?>

The above statement implicitly calls __set() with the value of $name set to “attribute” and the value of $value set to 5. You need to write __set() to do error handling.

The __get() function works in a similar way. If, in your code, you reference

<?php
$a->attribute
?>

this expression implicitly calls the __get() function with the parameter $name set to “attribute”. It is up to you to write the __get() function to return the value.

Access Modifiers Debunked

public – this is the default option. That means that failure to declare an access modifier on an item makes it public. The particular item will be accessible both in and outside the class.

private – this means that the marked item is only accessible within the class. You may also declare some methods as private – for example if they are utility functions used only inside the class. Items that are private will not be inherited.

protected – this means the marked item can only be accessed within the class. It also exists in subclasses! Think of this as halfway between private and public.

I am going to stop here then continue tomorrow. I hope you found this helpful. If you have any questions or errors you might have spotted, let me know. There are just a few more concepts that should fit into a single post bringing this OOP topic to an end. Stay safe and hope to see you soon! Please drop me a line in the comment section.

Functions exist in most programming languages and they separate code that performs a single well-defined task. This makes it easier to read and reuse the code. A function is a self-contained module of code that prescribes a calling interface, performs some task and optionally returns a result.

There are several reasons to write your own functions in PHP; for instance if you want to perform a task that other built-in functions cannot help you do. Let us quickly look at an example of a function call before we do function definition.

<?php
#call a function in php
function_name();
#this line calls a function named function_name that does not
#require parameters. This line also ignores any possible values
#that might be returned by the function.
#You might have come across phpinfo() function used to test
#php version, information about php, the web server setup etc
#it takes no parameters:
phpinfo();
?>

Most functions however do require a parameter or more – which are the inputs to the functions. If a function expects a parameter to be passed, you can either pass in the exact value or a variable containing the value. Let us see an example

The square brackets around use_include_path and context indicate that these parameters are optional.That means you can choose to provide values for optional parameters or you can ignore them in which case the default values will be used. Note that for functions with more than one optional parameter, you can only leave out parameters from the right. In our example, you can leave out context or both use_include_path and context but you cannot leave out use_include_path and provide context.

Knowing the above prototype therefore makes it easier to understand the code below as valid:

One common mistake among new php users is calling a function that is not defined. If you do that, you will get an error message! Always check to see if your functions are spelled correctly during definition and check to see if the function exists in the version of PHP you are using.

You might not always remember how the function names are spelled. For instance, some two-word functions have an underscore between the words and some don’t. The function stripslashes() runs the two words together while strip_tags() separates the two words with an underscore.

You will also get an error message if you try to call a function that is part of a PHP extension that is not loaded. It is a good thing to name your functions with lowercase for consistency.

The cool thing about programming languages is the ability to write your own functions. You are not limited to using built-in php functions!

Basic Function Structure in PHP

A function definition starts with the keyword function and then followed by a name, then the parameters required and contains the code to be executed each time this function is called:

<?php
#define a function
function my_function(){
echo 'Hello, my function was called';
}
#we can now call our function as follows:
my_function(); #you can guess the output!
#built-in functions are available to all php scripts but
#your declared functions are only available to the scripts
#in which they were declared. It is a good idea to have a file
#or set of files containing your commonly used functions. You
#can then use require() to make them available to your scripts.
?>

Naming Functions In PHP

The main point to consider while naming your functions is; short but descriptive. Consider a page header function – pageheader() or page_header() sound pretty good! There are restrictions to follow:

Your function cannot have the same name as an existing function.

Your function name can only contain letters, digits and underscores.

Your function name cannot begin with a digit.

As opposed to other languages, function overloading – the feature that allows you to reuse function names, in PHP is not supported.

If we run the above code, we get a much better table because we can adjust the parameter values as we wish. If we don’t supply them, the default values are used! This is what we get on the browser.

Understanding Scope

A variable scope controls where that variable is visible and usable. Below are the simple rules in PHP to follow:

Variables declared inside a function are in scope from the statement in which they are declared to the closing brace at the end of the function. This is called function scope. These are called local variables.

Variables declared outside functions are in scope from the statement in which they are declared to the end of the file but not inside functions. This is called global scope. These variables are called global variables.

The special superglobal variables are visible both inside and outside functions.

Using require() and include() statements does not affect scope. If the statement is used inside a function, the function scope applies. If it is not inside a function, the global scope applies.

The keyword global can be used to manually specify that a variable defined or used inside a function will have a global scope.

Variables can be manually deleted by calling unset($variable_name) .A variable is no longer in scope if it has been unset.

Pass By Reference vs. Pass by ValueLet us consider a simple function increment()

<?php
function increment($value, $amount = 1) {
$value = $value +$amount;
}
$value = 10;
increment($value);
echo $value # 10 - no change
#-----------------------------Solution-------------------------#
function increment(&$value, $amount = 1) {
$value = $value +$amount;
}
$a = 10;
echo $a; #10
increment($a);
echo $a; #11
#Explanation:
#-------------by value -----------------------#
#If you pass by value, a new variable is created containing the
#value passed in. It is a copy of the original. Modifying this new
#variable does not change the original value.
#----by reference-----------------------------#
#Instead of creating a new variable, the function receives a
#reference to the original variable.This reference has a variable
#name starting with a $ sign and can be used like any other
#variable. Modifying this reference variable affects the original
#variable.
#You specify a pass by reference parameter by using an ampersand(&)
#before the parameter name in the function definition.
?>

Using The return keyword:

The keyword return ends the execution of a function. Execution of a function ends either after all statements have been executed or the return keyword is used. Consider this example:

<?php
function test_return() {
echo "This statement will be executed";
return;
echo "This statement will never be executed";
}
#Calling the above function will cause the first echo
#statement to be executed and not the second one!
?>

The above example is obviously not the best way to use return keyword. Normally, you want to use it while checking for some conditions being met or not. Let us see a simple example here:

Implementing Recursion in PHPRecursion is supported in PHP. A recursive function is a function that calls itself. They are more useful when navigating dynamic data structures such as linked lists and trees. Example:

The above functions do the same thing but using recursive and iterative approaches respectively.
With each iterative call in the first function, the length of the string is tested. When you reach the end where strlen($string) == 0, the test fails. One issue with recursion is the memory use. It is up to you to decide which approach fits your taste.

As I end this post, I would like to direct you to PHP documentations where you can find out more. Thanks for stopping by and I hope you found some useful information here. If you have any questions, please let me know through the comments section. If you spot any errors, please let me know as well.

Hi! I am back from a serious headache and today am going to talk about regular expressions. The idea is to validate data submitted by the users of our online store. This will help us stop taking in invalid emails, or stuff like that. Also, using a new page created for feedback submission, we would like to send the message to respective departments depending on which words our customers mention in their messages. Let us get started.

Here is my feedback page:

Basically, we don’t want crazy people submitting fake data just to annoy us and to avoid that, we use regular expression. This is a huge subject and I will not exhaust it, instead, I will touch on the basics so that you can go out there for more details.

From the above code, you might have noticed certain new things: trim, addslashes and explode. When trying to store data in a database, you want to do some clean-up to remove any dangerous characters which might be interpreted by the database as commands. To eliminate such uncertainties, you use trim() and addslashes(). explode breaks the string given using the delimiter (‘@’) given and stores the results in an array.

Let us assume your customers were really outraged by your service and they just unleashed really mean feedback. To protect your employees, you want to replace every cuss word with some other word. Let us do that!

So far, we have not really used any regular expression functions yet!
I am going to make this as simple as possible to avoid confusion because Regular Expressions are not very easy to understand yet they are so useful!

We are almost done here. Now that we know some regular expressions, we should be able to use them to validate our data: particularly the email and the message- searching for keywords then determining where to send the message!

That is it for today. I hope you found this helpful in a way. If you have any questions, please let me know so that I can answer them. Next time we will tackle functions and code reuse. If you have spotted any errors, please let me know through the comments section and I will really appreciate.

Hello! Today, am just going to step a side(from code) and share with you some of the coolest things I have in my pocket! First of all, let us both agree that the web is too big to beat and you could spend the rest of your life roaming around in search for the right information!

I don’t know about you but I face information overload quite often and I end up not getting the answers to my questions even though I have the whole day to sit in front a computer. You are probably thinking, ‘what about just opening a browser and typing some key words into Google ?’ Yeah, trust me, you can do that and still find nothing useful.

Quite honestly, I think Google is doing a great job bringing us information when we really need it. But what do you do when nothing seems to work? You don’t have a friend to call and ask a question, you are too scared to ask your co-worker a private question, and the list goes on and on. Well, don’t sweat it! I have a recommendation for you! Quora!

Quora is not your typical website. You might say, well, Facebook is cool too or Google+ or even Twitter but you could be in for a big surprise! What I feel sets Quora a side is the quality of the information. Again, you might argue otherwise – this is my opinion. The idea that a company could be started, made available for FREE of charge and enable people from all walks of life to ask questions of all types and get answers from the smartest in their fields is just AMAZING. If that does not appeal to you, I have no clue what will.

Are you already asking yourself why I am praising Quora so much? The good thing is this; I have no affiliation whatsoever – I just like giving credit where it is due. You convince me that you are worth it and I say ‘good job kid’. The opposite is true.

So, I will make this post as short as possible so that you can visit Quora and take a look if you have not already been there! Good luck and if you have any ideas, great sites like Quora, please do not hesitate to share with me and the rest of my awesome readers.

Note: If you are teaching yourself how to code or just want to keep your skills up to date, I recommend checking out Mozilla P2PU Challenges and then hop on over to Smaterer where you can test your skills through a series of tests! You will love it.

Thanks for stopping by and remember to drop me a line and if you like what I post here, please subscribe!