How to retain value of a variable in PHP

Tags

, , , , , ,

DSCN0727//embedr.flickr.com/assets/client-code.js

For the most part, PHP variables have the single scope. It happens because PHP variables represent pointers that hold the addresses of memory areas with data. These data are actually assigned to the variables.

object-oriented-design-patterns-in-php7 for web

object-oriented-design-patterns-in-php7 for web


   

You cannot retain that value through functions. Any variable, used inside a function, is by default limited to the local function scope.

I’m a Full-Time writer. If you find these articles helpful please consider buying any of my books.
You can buy from either Leanpub or
Amazon or from my publisher
Apress.

However, we can solve these problems.
First, we see how things work locally and globally.


                    $value = 12;
                
                echo "We are calling value : " . $value;
                
                echo "Calling function that could not take outside value.;
                localFunc();
                echo "Calling function that can take outside value;
                globalFunc();
                
                function localFunc() {
                    
                    $value = 24;
                    
                    echo "This is inside value, defined differently : " . $value;
                    
                }
                
                function globalFunc() {
                    
                    global $value;
                    
                    echo "We are accssing outside value through this function : " . $value;
                
                }
                
Here is the output:

We are calling value : 12
Calling function that could not take outside value.
This is inside value, defined differently : 24
Calling function that can take outside value.
We are accssing outside value through this function : 12

Now consider this code again:


                //normally function does not change value
                // the value inside them is reset every time you call it
                
                function NormalFunction() {
                    
                    $value = 0;
                    $value++;
                    return $value;
                    
                }
                
                // now if we call this function many times, each time the value is reset to
                // the old value
                
                echo NormalFunction();
               
                echo NormalFunction();
               
                echo NormalFunction();
               
                echo NormalFunction();
               
                
                // how can we solve this problem?
                // the first way
                
                echo 'Using Global';
                function GlobalFunction() {
                    
                    global $globalvalue;
                    $globalvalue++;
                    return $globalvalue;
                
                }
                
               
                echo GlobalFunction();
               
               
                echo GlobalFunction();
               
               
                echo GlobalFunction();
               
               
                echo GlobalFunction();
               
                
                
                // the second way
                
                echo 'Using Static';
                
                function StaticValueFunction() {
                    
                    static $staicValue = 0;
                    $staicValue++;
                    return $staicValue;
                    
                }
                
               
                echo StaticValueFunction();
               
               
                echo StaticValueFunction();
               
               
                echo StaticValueFunction();
               
               
                echo StaticValueFunction();
                  
Here is the output:

1
1
1
1
Using Global
1

2
3
4
Using Static
1
2
3
4
Advertisements

Object Oriented Design Patterns In PHP 7

object-oriented-design-patterns-in-php7 for web

object-oriented-design-patterns-in-php7 for web


   

PHP 7 – is kind of a revolution in the way that delivers enormous power to everything from websites and mobile to enterprises and the cloud. This is the most important change for PHP since the release of PHP 5 in 2004. It has brought explosive performance improvements, drastically reduced memory consumption, and a host of brand-new language features to make your apps soar. This book makes you ready to take that revolution under your wing.

PHP 7 will be definitely faster, and speedier than before but that is not the thing we’re going to discuss about in this book.

What makes PHP 7 so special?

The very first thing is of course speed. It’s been found by the developers, around the world, php 7 is twice as fast as 5.6 and in some cases it’s faster! Since it’s tested on widely used php CMS like wordpress and drupal, you can bet on it.

The founder developers have been working hard on this issue since long time. Speed really matters and the developers always want one thing. The language code-base should reduce memory consumption and increase performance.

Php 7 has touched that dream-point.

How we can use that speed?
How we can use our object oriented programming concepts to exploit these cool features?
How we can use beautiful design patterns to make our php7 application simply ‘awesome’?

Who should read this book?

This book is intended for everyone. From an absolute beginner to the seasoned php developers who have frequently used procedural methods but have never tried OOP seriously.

Besides, for absolute beginners I have written a quick recapitulation that will guide you to the core concepts of OOP and Esign patterns.

Are you a beginner or an intermediate learner who has been struggling through the maze of OOP and Design Patterns concepts?

Then this book is for you

We are interested about learning new Features of PHP 7. They are Scalar type declarations, Return type declarations, Null coalescing operator, Spaceship operator, Constant arrays using define(), Anonymous classes, Closure::call(), Group use declarations and many, many more…

How to get 100 out of 100 in Google website speed test

Tags

, , , , , , , , ,

I dedicate this post to the webmasters, web developers who want to get good Google indexing and try to work hard for it.

You probably know about the Google webmaster tools; so I don’t repeat that part. If you are new, please remember, this is very simple. All you need is a Google mail account to sign in and start working on it. Webmasters typically use it for submitting websites and sitemaps, so that Google Bots could easily find them and index them accordingly.

As far as “search” is concerned, Google is over the top; no one around. So consider few aspects before you plan to create a website.

Whenever you create a website, it has some goals. It has some intentions. Niche audience. Viewers.

And Google tries to index them accordingly.

The days of keywords are gone. Now it depends on your content. Content for a website means text, image, and video. Try to make them unique and interesting; besides you need to follow the Google Optimization guide.

It says, your images should be in proper format. That means it ‘saved for web’. Resize and crop them accordingly. We hardly think about the size of the image or video. But it matters.

Be aware of the CSS and JavaScript files. Bots don’t feel happy if they have to travel deep into the folders to follow your CSS trail! They complain to the Google and they ask for optimization. Try to keep them as close as possible. It’s a good habit to write the CSS and bootstrap them into a single file and write it yourself. Write the responsive CSS style according to your needs. Avoid the copy-paste approach. If you don’t know, learn HTML, CSS, and JavaScript. It’s easy. It doesn’t take much time.

Knowledge of front-end development is extremely important if you want to take up the web development job seriously.

Make your website meaningful and let it open up for your viewers in dazzling speed.

Find the correct HTML path in PHP

Tags

, , , ,

You’re in a jungle. There are many trees and the forest is dense and dark. You’ve lost the path.
How will you get back to your home?
If you have a device, that can give your exact location, finding the way to home seems much easier. Isn’t it?
A complex website structure sometimes seems to be like a dense forest. And we think, oh, God, let me have a device that can locate my root easily.
Let me explain why it happens.

Continue reading

How to install Netbeans in Ubuntu

Tags

, , , ,

You need a good IDE to develop your projects. If you are a JAVA, C/C++, or PHP developer, Netbeans comes in handy.
For Windows and MAC there are separate guides and they are more GUI based. However in Ubuntu, I’d suggest you to better take the terminal route.
First of all you need to have at least 14 onwards Ubuntu version. The 12 version-updates are stopped since April, 2017.

Continue reading

how to upgrade to php 7 in Ubuntu

Tags

, ,

how-to-upgrade-to-php7

how-to-upgrade-to-php7

It’s a very hot-knob technique and takes not more than few minutes.
Recently I’ve installed and re-installed Windows and Ubuntu – side by side (please don’t consult Urban Dictionary to find the latest meaning of this old fashioned phrase, you’ll be shocked).
It was quite natural that Ubuntu sits on my home machine with old php version, that is, php 5 point something.
I’ve fixed with few simple commands that you may try while doing the same side-by-side :).

Continue reading

‘Panama Papers’ and ‘Paradise Papers’ Scandal: Romantic Technologists behind the Great Leak

Tags

, , , , , ,

It’s a story of ‘Fall of Man’ and the prevailing of romanticism.
When English poet John Milton wrote his epic poem ‘Paradise Lost’, in 17th-century, he had never imagined how his thought of ‘justifying the ways of God to men’ would be degrading down from generation to generation in such a sad way.
But, it really did; indeed! The recent leak of ‘Paradise Papers’ actually gives enough strength and succor to his favorite idea of ‘fall of man’; and the ‘ways of God’ has been rebutted when journalist and blogger Daphne Caruana Galizia  was killed recently in a car bomb near her home in Malta. This courageous lady was described as a ‘one-woman WikiLeaks’ by the Politico website. She led the ‘Panama Papers’ investigation into corruption in Malta and she had to be stopped; because her writings had been exposing the vicious nexus between politics and underworld in Europe’s smallest member state.
From ‘Panama Papers’ to ‘Paradise Papers’, a cycle is now complete. But, it’s just a beginning of a new era of journalism – data journalism; where ethical hackers play significant roles to expose hidden lies. Hidden files!
Continue reading

Windows and Ubuntu installation problem

Tags

, , , , , ,

Considering installation of multiple Operating Systems in your PC or Laptop? Then this post might come to your help.
I have recently gone through a deadening installation process of two different types of Operating Systems – one is Windows 7 and the other is Ubuntu 16 – in my desktop. It shouldn’t have been deadening; as it’s normally very easy; all you need to do is follow few steps and you’re done within few hours. I’ve gone through many formatting in my life and for the first time I made a mistake.
Experienced and advanced Linux users often forget that Windows is an extremely selfish operating system. It doesn’t recognize any other operating system. And that’s the typical Microsoft way of thinking – we’re the king; there shouldn’t be any other partition except their own.
Continue reading

A letter on Laravel from Vietnam

Tags

, ,

I’d like to share a letter which I’ve got few days back and I think, it’s really inspiring for any author.
The letter came from Vietnam and the reader wrote it after reading my book “Beginning Laravel” by Apress.
Continue reading

How ethical is it to use silicon dolls?

Tags

, , ,

silent as the dead

silent as the dead

Originally published in TEHELKA

sex-dolls-with-artificial-intelligence – You can download the PDF to read offline.

Meet Dr. Kathleen Richardson.
She is a ‘Reader’ and senior research fellow in ethics and culture of robots and Artificial intelligence in De Montfort University Leicester. She is also one of the most vocal campaigners against sex robots. Being one of the many activists who have launched full scale scathing attacks against using silicon dolls – with customizable labia and hips; she has made her points stronger by comparing it with ‘owning slaves’. But, at the end of the day, who is listening to her? At least, not the male chauvinists.

I’m a Full-Time writer. If you find the articles helpful please consider buying any of my books.
You can buy from either Leanpub or
Amazon or from my publisher
Apress.

They are ready to spend fifteen thousand dollar for a sex doll which is ready to adopt fifty nine positions.
Continue reading