Posts

Showing posts from May, 2025

IF control structure in PHP

It is possible to execute specific code conditionally. This is done using if statement if statement. This statement includes code or condition. If this condition evaluates to true, than code within if statement will execute. It is possible to have alternative cases. In PHP it is represented with else if keywords. There could be zero or more alternative cases. Each case will have specific condition to try. If it evaluates to true, then the code that it within that condition will execute, otherwise flow of the program will continue to the next case and so on. It is possible to have optional default case. This case does not have any condition. It will execute if all of the cases above it evaluate to FALSE. In PHP it is represented with else keyword.

Restrictions on characters to be used in HTML file names

HTML standard prohibits use of certain characters in file names. For example tab, or space, or angle brackets are not supported by web browsers, and these characters need not to be used in names of HTML files.

Support for HTML5 format by different browsers

Different browsers have different support for HTML5 standard. Some browsers support this standard more and some less. Common HTML5 attributes are well supported by all browsers. Browsers may differ on how they represent little HTML 5 details. As a person that develops a web site, you need to be aware of these difference and make web site functional across the browsers that have majority of traffic to access your web site. Why am I saying majority of traffic and not all, because there maybe a user that use archaic version of a browser. It does not mean that that user needs to be supported. Majority of web sites will be broken to that user, and that user needs to upgrade. As a web developer or a person that support web site you need to be aware which browsers access your web site, and you need to ensure that web site works well with those browsers.

Common HTML terms

These are common HTML terms: Element Attribute Tag Element is a part of HTML code. For example a link to another webpage is represented with the element <a>. Attribute is the property of that element. For example in case of a link it a web page where that link point to. Attributes have name and value pairs. In the case of a link, attribute is href and value is a page that it points to. Tag of an HTML element indicates the end of that element. HTML tags are represented with forward slash in front of that element name.

CSS - Cascading Style Sheets

Even so it is possible to specify how HTML elements will look like in the HTML placing styling information in HTML document itself  is probably not a good idea. For example if body font size needs to be increased, then a person needs to visit every page and make adjustment there. To separate HTML elements from how these elements should look like CSS (CSS stands for Cascading Style Sheets) was implemented. CSS separate how the HTML elements will look like from the content. It is possible to link external CSS file to HTML page. This usually done via adding link section pointing to the CSS file in the head section of HTML file. Please not to forget to point all of the HTML files that need this style to this document.

Absolute and relative path in HTML pages

HTML documents can have relative or absolute links to other web pages. Relative links keeps a visitor on the same web site. Relative links are pointers to other web pages on the same web site. They just contain this page filename. Absolute links on other hand contain full path including protocol and domain name to access the page. Absolute links can point to the same web site or they can point to a different web site.

Design web content for people with disabilities

There are a lot of people in the USA and across the world that are people with disabilities. I am not talking here just about disability of moving a hand or a leg. Disability may include inability to hear. Imagine that a video is playing, but there is no sound. This is the world of a deaf person. There are a lot of people with disabilities and excluding those people from accessing web resources means that you are loosing visitors to your web site. Including closed captions in a video that indicate that music is playing will provide information to a deaf person of what action is going on now. Including text of a voice in a video is another good option to do.

HTML hyperlinks

HTML hyperlinks are denoted with <a> symbols to indicate the beginning of one, and with </a> symbols to indicate the end of it. Within <a> tag it is possible to have a few options: href will indicate which page or section of a page needs to be opened after a link is clicked. target equal to _blank will open this link in a new page. For a full list of targets, please visit  https://www.w3schools.com/tags/att_a_target.asp page.

Italicizing text in an HTML page

Italicizing text in an HTML page is done using <i> tag. <i> tad indicates the beginning of italicized text, and </i> indicates the end of it.

Making text bold in HTML

It is possible to make text standout in HTML page by using <b> tag or <strong> tag. Even so text appearance will look the same from a visitor perspective, <strong> tag points out important information for crawlers. To indicate end of bold text is done using same HTML tags, but with a slash in front of one.

Paragraphs in HTML

Paragraphs in HTML are denoted by <p> to indicate beginning of a paragraph and </p> to indicate the end of it.

Images in HTML

Images on a web page can be a different type. Modern browsers support many image types. The images can be GIF images, JPEG images, BMP images. When an image is included in a web page, please consider image size. Even so it is possible to reduce the image size in the HTML, it maybe a bad idea to transfer a lot of bytes in a wire. It is possible to reduce image size ahead of time to lower the amount of data which will be transferred.

Textual information for images

In the beginning HTML pages did not have images, it was text only. Nowadays we enjoy pictures, music even videos in the Internet. To accomplish functionality of what the image is supposed to be there is ALT HTML tag for links. Tag is not the same as HTML element. Tag goes inside of HTML element. If there is a picture, ALT tag can describe of what picture it is.

Validating HTML

It is also easy to make mistakes in HTML code when a person does not have experience creating HTML code. To uncover issues it is possible to validate if HTML code adheres to the HTML standard. In order to do so, it is possible to copy and paste HTML code in https://validator.w3.org/. Please note that, if HTML code is private and ment for internal use only, then using this validator means that HTML code will be send across public Internet. Even so this website uses encryption, you need to be careful of sending data across the Internet.

Ordered and unordered HTML lists

It is possible for n HTML page to have ordered and ordered lists. Ordered list will have a number in the beginning of every new item. Unordered list will have a bullet point. Unordered lists will start with <ul> table definition. Ordered lists will start with <ol>.

Validation of HTML structure

It is a good idea to validate HTML content that was created. One of the tools that can assist in doing so is a website validator.w3.org. Please note that this information may become publicly available and if you are creating internal web site, than it is a bad idea to use publicly available services, since the information that you transmit may become available to others. I am not saying that validator.w3.org service will make this information available to others, but the information that you transmit over the Internet can be intercepted by others.

Footer section of an HTML page

HTML page may contain <footer> section. This section is optional. It helps search engines and assistive technologies to understand what this page is about. If content on a page is not clearly understandable it is a good idea to rewrite this page.

Placement of HTML files

Placement of HTML files needs to be logical. For example it is a good idea to keep JavaScript files in one place, images in another one. It is a bad idea to mix these all together. HTML files also are responsible for specific types of web site functionality. It is a good idea to organize HTML files in folders which are responsible for this functionality.

Header HTML tag

HTML head tag includes HTML page title information, links to style sheets, character set to be used, links to JavaScript files.

Special entities in HTML

Bracket is a special symbol in HTML. Opening bracket defines starting of HTML code, and closing bracket shows where HTML code ends. However, what to do when brackets need to be shown? HTML defines a few words to define special characters. These words are; &lt; is used for opening bracket, &gt; is used for closing bracket. There additional special HTML words that can be used in place of characters. For example space is represented in HTML as &nbps; . A browser will ignore extra spaces and will show only one of them. If more spaces need to be shown on a web page, then extra spaces need to be replaced by &nbsp; If ampersand needs to be shown in an HTML page, then &amp; needs to be used. There are additional HTML symbols, I just mentioned a few common ones.

HTML IDs

Or is possible to assign unique ids to HTML elements. I repeat it one more time because it is important. IDs of the HTML elements must be unique. HTML page will work even with duplicate IDs, however any sort of assignment of properties to element by ID, or working with HTML elements by ID using JavaScript will be broken.

Broken HTML images

It is possible to have broken HTML images. There are couple of reasons for that. Reason number one is that the wrong image path was specified. Reason number two is that image is no longer available. It can be different from reason #1, because the image may had been there and HTML page looked correctly, however the image got deleted and HTML page will look weird.

Ordered and unordered HTML lists

It is possible to make a list of elements in HTML by specifying <ol> or <ul> tags. <or> tag stands for ordered list and <ul> tag stands for unordered list. Ordered list will be presented with numbers. Item number one, two and so on. Unordered list will be presented with bullet points.

Line break in HTML

Line break is represented with <br> HTML tag. It also a self closing HTML tag.

Introduction to HTML

There are opening and closing HTML tags. There also self-closing HTML tags. For example <img> tag is self closing. Closing of HTML tag needs to follow its opening. If other HTML elements are placed inside of that element, this is how they will be displayed. For example an image tag maybe placed inside of HTML table. When an HTML page is rendered then, tho is how those elements will be looked like. Closing of HTML tags must be done in proper order. If this is not done in order, than it will be a broken HTML page. HTML elements may have attributes associated with them. For example width of an HTML table is one of the attributes.

Introduction to CSS

CSS stands for Cascading Style Sheets. CSS specifies how objects in HTML document will look like. What type of font of the content will be: what style, color and size. How the links will look like, how HTML tables will look like and so on. You got the idea. CSS makes decoupling development and design tasks. Even so web developers are good at developing an HTML page, it doesn’t mean that they are good at styling it. Functionality of a web page is not same as styling. Styling requires totally different skill set. Styling of a web site may require color scheme and looks of the brand.

Validating HTML document

Browsers are nice dealing with errors, they try to render things which you had ment. It does not mean that we can write sloppy code, we need to ensure that we had written well formatted HTML document that adheres to standers. To perform that validation it is possible to navigate to validator.w3.org to ensure that HTML document does not have any errors. Of course if it is an internal only page and needs not to be made public then it is a bad idea to transfer that information.

Three parts of HTML document

HTML document has three parts. These are DOCTYPE, HEAD and BODY. I already explained what DOCTYPE is. HEAD part of the HTML document will have information that is not visible in the content of the document. This information includes title of the page, links to external resources such as CSS files to be used, metadata about this document such as authors, description, keywords. BODY part is a content of HTML documents that is visible by visitors of that web page.

Doctype

Doctype is a first element in HTML documents. It tells browsers which version of HTML to use. Doctype is optional. By default browser will use HTML 5 as a document type. Of course if you have a browser that was published before HTML 5, then this browser will not support HTML 5 specific format. However if you have a browser that old then you probably have all sort of issues browsing the Internet. DOCTYPE had not seen any new updates for a while.

Code branching with IF statements

Code can be branched with the use of if statements. If statement has one or more conditional statements. If it has more than one conditional statement than it is possible to specify which condition needs to applied to it. It can be AND, OR. This condition can be negated. There could be more than one additional condition, but each additional condition will make if statement more complicated. That code of the IF statement will be executed, where the condition is evaluated to true. There maybe additional conditions that will evaluate to true, however these will be ignored, because the condition was already satisfied. Only the first expression that is evaluated to TRUE will be used. More complex conditional statements can be made with PHP's keyword case . Other programming languages may use different name for this keyword. On flowchart diagram IF statements are represented with the rhombus.

Timestamp

Timestamp is representation of time since January 1st 1970. This date was chosen arbitrarily, there is no special event that is tided to this date, it was simply a convenient date to work with. On January 19, 2038 32-bit systems will have 32-bit overflow and count will start again. It is important to upgrade to 64-bit systems before that date.

range PHP method

It is possible to create a list of numbers in PHP using range method. range method takes three arguments, These are beginning number, ending number, and step. Step can be negative to indicate that this sequence must decrease. Step is optional argument, by default value of a range is equal to one. The return of this method is an array of numbers.

HTML elements

HTML content is enclosed within opening and closing HTML brackets. Closing HTML tag has forward slash symbol that follows opening bracket. Example of opening and closing HTML tag is an <a> tag that is a link a defines which page to open when the text that follows <a> tag is clicked. Content of the <a> tag what text will be displayed for that link. There can be a number of HTML elements. These are used to display an image, a horizontal line, a button. Some HTML elements are self closing, there is no need for closing HTML tag.  These elements include: <br> or line break, <hr> or horizontal line, <img> or displaying an image. This is not a full list of self closing HTML tags, for full list of HTML tags including self closing HTML tags please refer to HTML documentation.

Refreshing HTML content

When changes are done to the HTML page you are working on, it does not mean that these changes will be shown immediately. It is necessary to refresh that web page to see these changes. It is possible to auto refresh a web page using http-equiv HTML head tag, or by using  JavaScript.  I assume that this is a simple HTML page and there is no auto refresh there.

Web hosting

Web hosting is a hosting of the web files on a server with a fast internet access. There could be two types of web hosting: shared hosting and dedicated hosting. Shared hosting is when many web sites exist on a single machine. Shared hosting is usually cheaper than dedicated hosting. Dedicated hosting is when a machine is used only by a single account. 

Converting a string representation of date to Date format

It is possible to use strtotime method in PHP to convert human readable representation of date and difference between dates to Date format. Example of date descriptions that can be given to this command are: now, plus or minus so many days, next Wednesday, last day of the month.

Determine difference between two dates in PHP

It is possible to determine difference between two dates in PHP using diff method. This methos is part of the actions of the DateTime object. Two datetime objects need to be created with date and time desired in to use diff method, This method  will result in an object, which will contain properties with difference in years, months, days, hours, minutes and seconds. In addition to this detailed information, it is possible to find out total difference in days between these two objects using days property.

Setting a date in PHP

Setting a date in PHP is done using setDate command. This will set a date to DateTime object. DateTime object can be created by using date_create method.

Merging arrays in PHP

It is possible to merge arrays in PHP. This done using array_merge method.

Array intersection in PHP

It is possible to find common elements between arrays in PHP. This is done with array_intersect, array_intersect_assoc and array_intersect_key functions.

Relational and non-relational databases

When database choice is to be made, then it is possible to choose from two options. These options are relational and non-relational databases. Relational databases ensure consistency of the data stored in different databases tables. For example customer orders will ensure that customer record exists in the database. Non-relational databases offload this functionality onto application front-end. It responsibility of the application to ensure that customer record exists before the order is saved. If a web site becomes popular and receives more traffic then it is needed to install more performing machine in a case of relational database. In case of non-relational database back-end, to handle more load is done by adding more servers. Non-relational database scale better than relational databases, and it is a cheaper option. There may be time when relational database reaches the point of maximum capacity. To handle more load database records can be split.

Web design and web development

Web design is different from web development. Web design means how things look: what is a color scheme, what is the layout of the web page elements, what fonts are used. Web design means how to differentiate your web site from a million of other web sites. Designing a web site requires good artistic skills. Web development on other hand is responsible for logic of the web site. It is possible to split web development onto two parts: frontend development and backend development. Frontend development is what a person sees when that person visits a web site. Backend development is development of databases and logic that is used by frontend. Both jobs are stressful. Because an issue with the website is visible by the website visitors. It is example of an issue with frontend development. An issue with the backend will impact functionality of the website. For example items purchased by one user may end-up being reported in another user shopping history.

Making a website consistently looking with the use of CSS

Even so HTML is good for creating structure of the website it is a good idea to use CSS to decorate elements in HTML page a specific way. CSS stands for cascading style sheets. It has a name cascading because properties of the parent element will propagate to its children. For example if a specific font is specified at the body element. Body element is being higher in the hierarchy. This font type will be applied to the children of the body element, this is good for consistency design of HTML elements. It is possible to override the design characteristics for specific elements.

Making a website dynamic with the use of JavaScript

It is possible to use JavaScript to make websites dynamic. Even so HTML is good for displaying static information, users may want to see things change when specific activity is done or just by looking at the website. To make a web page look more dynamic JavaScript can be used. It has term Java in its name, however it has no relationship to Java that is developed by SUN Microsystems. For example there could be snowflakes falling on December 25th in the web site or background of the web site can turn black at a nighttime.

HTML and CSS

HTML data represents content and layout of the data. Even so it is possible to specify how the data will look like, there is more efficient way of doing so. It is possible to specify characteristics of the content in a CSS file. HTML content will just refer which style from CSS file to use for which component. This way web page will look more consistent.

Hosting a web site

To host a website it is needed to have hosting. It depends on the complexity of the website and the volume of traffic which machine you need to get. If volume of web traffic is low, then a single virtual server maybe sufficient, if web traffic grows then it may be necessary to get better virtual machine or even dedicated server. If website traffic grows even more, then you may need to think about getting several hosts. If traffic grows even more, then it maybe necessary to invest in host in several geographical locations. These locations need to be close where your users are. It is possible to determine geographical location of the request, and send that user to closest geographical location to minimize latency. To comply with laws of a specific country, server may need to placed in the physical location of that country.

Merging and concatenating PHP arrays

It is possible to merge associative arrays using array_merge method. If this method to be used on indexed arrays, the values of the first array whose indexes are equal indexes of the second array will be replaced.

Custom sort of a PHP array preserving keys

It is possible to perform custom sort of PHP array elements and to preserve keys associated with the values of that array. This is done using uasort method.

Custom sort of a PHP array elements

It is possible to create a custom function to order array elements. Calling this function can be done usort method. This method takes two arguments: array to be modified and name of the function.

Randomize order of elements in a PHP array

It is possible to randomize order of PHP array elements using shuffle method.

Sorting of PHP array in descending order by key

 It is possible to sort a PHP array in descending order by key. It is done using  krsort  method.

Sorting of PHP array in ascending order by key

It is possible to sort a PHP array in ascending order by key. It is done using ksort method.

Sorting of PHP array in ascending order preserving indexes

It is possible to sort values of PHP array in ascending order and to preserve indexes. This is done using asort method.

Sorting PHP array in descending order

It is possible to sort values of a PHP array in descending order. This is done using rsort method.

Sorting PHP array in ascending order

It possible to sort a PHP array in ascending order. This is done using sort method. Values of the array will be sorted in ascending order.

Removing last element of the PHP array

Similar to array_shift method, array_pop method will remove last element of a PHP array,

Removing first element of the PHP array

array_shift method can be used to remove first element in a PHP array. array_shift method does not return any value, but rather modifies the array in place. array_shift method takes only one argument, this is array name.

Removing PHP array element

It is possible to remove PHP  array element. This is done using unset method. Unset method takes array element which needs to be removed. The array can be either indexed or named array. This method takes an argument which array element needs to be removed.

Removing custom values from a PHP array

It is possible to use array_filter method to remove custom values from a PHP array. This function takes two arguments. First one is name of the array, and the second one is a function that need to be applied to an array element. This function needs to return either true or false. True statement will indicate that element of the array needs to be kept and false means that it should not be kept. array_filter returns a new array.

Removing empty values in a PHP array

It is possible to remove empty values from a PHP array by using array_filter method.

Modifying a PHP array with array_push

array_push PHP method will modify an existing PHP array by adding new values to it. array_push method takes several arguments. First one is an array name, the following arguments are the values that are needed to be added to this array.

HTML and CSS

HTML is translated as Hypertext Markup Language. HTML is responsible for content that will be on a web page. Even so it is possible to specify how the content will look like in HTML document, it is a better idea to use CSS (Cascading Style Sheets). CSS file is a separate document and it is added to HTML document by using link keyword.

Type casting

It is possible to convert from one data type to another one. Of course if a value of a data type can be converted. For example it is possible to convert a string “3.5” to a number 3.5. However it is not possible to convert a string “ABC” to a number, because ABC cannot be represented by a number.

NULL value

NULL value is an absence of any value. You can think of an empty jar. Jar probably contained something before, however it does not contain anything at current moment.

Flowcharting

A flowchart is a graphical representation of a program flow. A flowchart has the same components as a program. It is a good idea to make a flowchart before starting coding. A flowchart achieves two results: A flowchart can reveal cases unknown to self. A flowchart will represent others what will be done. Is there a case when a flowchart is not needed? Yes. Flowchart is not needed for iterative development, when incremental changes are done to the code.

Variables

Variables in code has type and in case of numeric type they have precision. Common data types are: Boolean (these holds true/false data), integers, floats (there could be more than a single data type for floats, it depends on level of precision that is required). More precision comes at a cost of memory utilization. Another data type is a single characters. Another data type is a string. Even so, it is possible to use string data type to represent a character, dealing with strings have more overhead than dealing with single characters. It is a good idea to use single characters in cases when you know that no more than a single character will be used.

Comments in code

Comments in code explain why specific logic was used, and what the code does. Comments are for other people who look at the code. Comments are ignored by a machine and don’t impact performance of the code. There could be single line comments. These comments are simple. There are also multiple line comments. These explain more complex functionality.

No-SQL databases

Establishing relationships between data in SQL databases comes at a cost of performance of the database server. When it used a lot, then usually scaling up, or getting better hardware is usually the only available option. To overcome this limitation NoSQL databases were invented. NoSQL databases do not track data dependencies. This work is done by the application. Because of lower overhead of the database server, these databases are faster than SQL databases. Relationship of the data is ensured by an application rather than database server. When database is busy, then it is possible to scale horizontally. Scaling horizontally means getting more hardware. There are two main reasons of scaling horizontally vs. scaling vertically. Horizontal scalability is a better option than vertical scalability. It is possible to add more servers rather than getting a faster one. Horizontal scalability cost less than vertical scalability. In vertical scalability it is necessary to get a faster machine....

SQL - Structured Query Language

SQL is a standard and language to use with relational databases. SQL stands for Structured Query Language. It is possible to retrieve, update or add information using SQL. When updating records, it is possible to impact only those that confirm to a condition of WHERE clause. It is possible to establish relationships between several tables. In SQL it is done using equality between tables using data that is stored there. Example of SQL databases are MySQL, Microsoft SQL database, Oracle. There are other SQL databases. I just name a few. An opposite of SQL databases are NoSQL databases. NoSQL databases do not establish relationships between tables. Relationship between data is done via application. Advantage of using NoSQL database is speed. Because of less complexity NoSQL databases are faster.

Language of a browser

HTML is a language that browser uses. HTML stands for Hyper-Text Markup Language. HTML presents well static information. However we are able to listen to sounds and watch videos in the Internet. It is possible to include these sort of information in HTML.

HTTP request

HTTP request is way to request specific information from a server. HTTP request consist of three parts: First one is a protocol to be used. In a browser there are two protocols that can be used: HTTP and HTTPS. Letter S at the end stands for secure. Information between a browser and a server is encrypted. Only people that have encryption key are able to see that what is being transmitted. Encryption comes at a cost of higher CPU utilization on server side. As a user you may not notice higher utilization, and a single request should not cause utilization in a server to spike, however when there is a lot of traffic, then it is possible to notice higher utilization by HTTPS. Second one is a server . It can be a name or an IP address. DNS (stands for domain name system) is responsible to translate names into numbers. A person is good with names, however a computer is good with numbers. Thirds one is a file name . File name may contain folder path, or filename can be empty. If it is empty ...

Saving PHP errors in a file

In a production environment it is a bad idea to display errors, since errors may display too much of internal technical information. However certain errors will be produced in production environment only. Developers and quality assurance people simply were not able to reproduce error conditions. In such a case it is possible to save errors in a log file. This is done via error_log command. This command has several options of how to deal with error messages. Please refer to the PHP documentation of how this command needs to be used. Please ensure file permissions that it is available to write to this file. If there is more than a single server that saves errors in a log file, please consider aggregation of errors to a single location, it would be a pain in a neck to chase errors from server to server. Why this can be done this way? Because in production environment developers may not have access to ini file, however they still may need a way to troubleshoot issues. Logging Production Er...

Displaying PHP errors

It is possible to display PHP errors by setting display_errors flag to on in php.ini file. Please note this can be changed only for developers or QA people. In production environment this flag needs to be set to off, there is no need for customers to display a reason why application had failed. It is also possible to display PHP errors by specifying ini_set('dispalay_errors', 1). This can be done in an environment, where there is no access to php.ini file, however it is necessary to debug PHP errors. This setting is not global unlike settings in php.ini file, this setting applies only to a single file. If debugging information needs to be displayed in more than one file, please consider making this changes in an include file that is used across entire web site.

Assigning array element values to variables

It is possible to assign array values to variables. In order to do so variables need to be placed in square brackets. Later versions of PHP used array() function to indicate that values inside that function are part of the array. It is possible to create both indexed and associative array using this method.

Constructing a string from PHP array values

It is possible to construct a string from array values. In order to do so a method implode needs to be called. This method takes two arguments. First one is a separator between array values and second one is the array variable.

Looping through elements of PHP array using iterator

First of all iterator is initialized with getIterator() method over an array. Main of the advantages of using iterator over looping through all of the elements of an array is lower memory consumption.

Modifying PHP array values In a loop

It is possible to iterate over a PHP array in a loop using two ways: First one is to access array elements using copy of the array. Second option is to use reference to the original array. If an element of the array is modified in a loop, then the original array will be unchanged, however if accessing of array is done via reference, then original array will be modified. Accessing array by reference is done using ampersand sign before the name of the array.

Identifying key of the array element

It is possible to use key method to find out which key array element has. The value of this operation will be null, if array iterator reached the end of the array.

Counting how many elements a PHP array has

It possible to find out of how many elements a PHP array has. In order to do a function called count needs to be used. Please not forget that count of PHP array elements starts with 0. Even so it is possible to find out number of elements in PHP array, last element in it will be count of array elements minus one.

Looping through all of the elements of PHP array using foreach loop

It is possible to loop through all of the elements of a PHP array using foreach method.  If a loop is done using foreach method, then it is possible to modify elements of an array using & (ampersand) sign in-front of the array variable. Example of this is: foreach ($array as $key => &$value)

Retrieving a value of a specific element of an array in PHP

It is possible to specify which element of an array needs to be retrieved in PHP. This is done using placing an index number in square brackets after the name of the array. Please note that count of elements of an array in PHP starts with zero.

Checking if a value exists in a PHP array

It is possible to use PHP’s in_array method to determine if a value is in array or not. If a value is in array, then this method will return true, it will return false if it is not there. It is also possible to use array_search method to check if the value exists in an associative array.

Creating a PHP array from variables

It is possible to use compact PHP function to create array from variables. compact function accepts the names of variables as parameters. These names are string names of variables. The resulting array will contain names of variables as keys and values of variables as values of those array elements.

Creating a PHP array using variable names and values

It is possible to initialize a PHP array using compact function. compact function accepts names of the variables as a text. Result of this operation will be an array which keys are variable names and values are values assigned to these variables.

Checking if PHP variable is an array

It is possible to check if a PHP variable is an array or not. To perform this operation is_array method needs to be used. If PHP variable is an array then this function will return true, and it return false if it is not an array.

Checking if an array key exists using PHP

PHP has several methods to check if an array key exists and if it exists was its value had set. These methods are the following: array_key_exists, isset, empty.

Initializing an array in PHP

Initialing an array in PHP can be done by two different ways. First one is to use keyword array, and second one is to use square brackets. Using square brackets to indicate that it is an array is available from PHP 5.4

Arrays in PHP

Arrays in PHP can hold more than one value. Arrays in PHP can have keys and values associated with these keys. Keys are optional in array. Array may hold values only.

Passing elements by value and by reference in PHP

Passing a variable in PHP by value means to use values only. Passing a variable by reference means modify the same variable. Passing a variable by value does not modify that variable. Passing a value by reference is not limited to variables only. It is possible to pass by reference classes too.

Errors

It is a good idea to send errors in production environment to error log. Errors may contain sensitive information. Even so errors maybe helpful to development team or to QA team, errors need to be suppressed in production environment for viewing by everyone. Errors are good way to troubleshoot an issue therefore only trusted people can have access to this information. If not a trusted person get an access to this information, this information maybe used against the company and for a personal benefit. What to do in a case when there are more servers than one? Even so it is possible to inspect the log files in a single server, however it is impossible to do so when there is more than one server is available and web traffic is redirected to a different server each time. Log aggregation is needed in this case. All of the logs will be send to a central location. Logs may contain sensitive information. Please consult your company lawyer of how long logs can be stored, because logs can be use...

Boolean operations

There are a few Boolean operations that it is necessary to know. These are equal operation which represented by two equal signs. It is possible to perform less than or greater than operations. These represented by less than or greater than signs. It is possible to perform less than or equal operation or greater than or equal operation. These operations are represented by adding equal sign to the right of less than or greater than operations. It is possible to negate equal or equivalent operations by placing explanation point in front of such operation.

Association or assignment

Association or assignment operations are done via using the equal sign. Result of the operation on the right side of the equal sign will be assigned to a variable on the left side. It can be mathematical, Boolean or a string operation.

Basic assignment in PHP

Basic assignment in PHP is done via equal sign. Variable to the left of equal sign will be assigned the result of operation on the right side. It can be not only mathematical operation, but a string or Boolean operation.

Altering the order of mathematical operations with parentheses

Since parentheses have higher mathematical priority than other operations, then it is possible to alter the order of mathematical operations with these. For example addition of two numbers have lower priority than multiplication, however placing addition operation inside parentheses will make this operation a priority. 2*(3+4) will be equal 2*7, 3+4 operation needs to be done first.

Order of mathematical operations

You are probably familiar with such a saying as PEMDAS. PEMDAS stands for parentheses, exponents, multiplication, division, addition, subtraction. Same order of mathematical operations are applicable to mathematical operations in PHP.

Combined assignment operations

It is possible to make a shortcut by combining both assignment operator with mathematics operation such as addition or subtraction. Adding a number and assigning the result will be represented by += operation. Similar to above it is possible to perform subtraction. Such operation will be represented by -= signs. It is also possible to perform multiplication or division operations in a similar manner. One more operation that is done this way is modulus or remainder operation. It is represented by percentage sign.

Boolean == and === operators in PHP

There are two equality Boolean operators in PHP. There is == Boolean operator which is represented with two equal signs, and there is === Boolean operator that is represented with three equal signs. The difference between them that the first one will check only the value, and the second one will also check the type. For example in the first case integer value zero will be equal to floating number 0.0, but the result of second operation will be false. Even so the value is the same, but this value is represented by different data types.

Setting a variable to NULL in PHP

It is possible to set variable to NULL in PHP. NULL represents absence of any value. Setting a variable to NULL is done via: $myvar = null;

Errors in PHP

Displaying PHP errors are good for debugging, they are good for development and QA departments, but errors may provide too much information in production environment. In php.ini it is possible to control how much debugging information is displayed. Setting error_reporting = E_ALL will provide all of the errors. It is possible to write PHP errors in a file php.ini needs to have the following settings:  error_log = ./errors.log where error log name is path and filename for the error log file. Setting error_reporting = 0 will turn off errors. It is a good idea to aggregate errors from multiple machines to a single location in a production environment, if it has more then one machine,

My journey to computer science

I was not a system admin when I was born, nobody is born with that title, everyone expects cute little  babies when they are born. Just like many of you I don't remember the moment I was born, it is a fact  that my memory doesn't remember things from my early childhood. I remember a picture taken in my childhood. I found that picture later inside the couch that we had. I remember that we lived in a city but  close to a zoo, which was nearby and the zookeeper fed the animals at a specific time of the day, and  the animals in the zoo made a noise when they expected feeding time or saw their food. I remember  that I was waiting for my cousins to show up, so I could play with them. When I was in my teen years, I did not have a computer, the only thing I was given by my parents at that time was a programmable  calculator, so if you don’t have a computer now, it does not mean that you never will have one, you  may not see a datacenter but it is not the end o...

Who was the first hacker, and it is written about him?

Who was the first hacker, and it is written about him? It was Samuel. He hacked Agag. 1 Samuel 15:33

return keyword in PHP

return keyword in PHP will cause halting execution the rest of the code of a procedure and it will cause continue execution of a code after that procedure was called.

Including other files in PHP

There are couple of keywords in PHP that will bring functionality from external files to the main code. These keywords are include and require . Difference between them is that by using keyword include the rest of the code of that program will continue to execute, if the file that is needed to included is absent. require on other hand will fail the execution, if the file that is needed to be included is absent. More than one file can be included for a program to operate. It is a good practice to use include files, since it is possible to validate functionality of the code that is being included. Including other code is beneficial since functionality of that code can be independently validated.

Negating a result of a Boolean expression

It is possible to negate the result of a Boolean expression in PHP by using keyword NOT. in this case True will become False, and False will become True.

Boolean operations

There are couple Boolean operations that you need to know. Boolean operations are used to evaluate result of several Boolean expressions. There is AND Boolean operations. In this case all the Boolean expressions need to evaluate to True in order for the final result to be True, if one of the expressions is false, then the final result will be False. If one of the expression is False, then there is no need to check the result of the other expressions, final result will be False. Another common Boolean operation is OR. If one of the Boolean expressions will be evaluated to TRUE, then the result of OR operation will evaluate to TRUE. If one the conditions evaluates to TRUE, then there is no need to evaluate the rest of the conditions.

While loop in PHP

While loop in PHP is represented by a keyword WHILE. The condition of the loop is a Boolean expression. It usually done with a variable that has a conditional to validate if that variable is less then or greater than a specific value. It is needed to change the value of this variable inside of the loop to avoid infinite loop.

If conditions in PHP

It is possible to define execution of different code based on the conditions. The conditions (yes there could be more then one condition) need to evaluate to true in order for the code within that condition to be executed. If that condition evaluates to false, then the code within elseif or else sections will be executed. elseif section specifies a different condition. elseif section is optional. Code within elseif section will be executed in case when that condition evaluates to true. There maybe more than one elseif section. Another optional section of the code is else section. It is the last section in the if statement. else section is executed in case when all prior conditions evaluate to false.

Precision of mathematical operations in PHP

Floating point numbers have limited precision, if you working with very long floating point numbers and precision is important to you, please be aware of it. It will also manifest itself in precision of mathematical operations with such numbers.

Comments in PHP

Comments in PHP can be of two types. There can be single line comments and multiple line comments. Single line comments are indicated by two forward slashes. Such comments can be on a separate line or they can follow the code. Multiple line comments in PHP are denoted by /* as to open a comment, and */ to close it. Comments don't execute. Purpose of comments to explain what the code does.

Knowing which class and method had been called in PHP

Similar to the __FUNCTION__ call it is possible to make a call to __METHOD__ built-in function. It will return both name of the class and name of the function. Personally I don't comprehend this.

Knowing which function had been called in PHP

It is possible to use __FUNCTION__ method to know which function had been called. Personally I don't think that this functionality is needed. A person, who codes a program will not randomly chose a function.

Array as a constant in PHP

It is possible to declare an array as a constant in PHP. In order to do so const keyword must precede the declaration. One other way of achieving the same result is to use define function. define function takes two arguments. First one is the name of a constant, and second one its values.

Using PHP's built in constants function

If it is unknown if a constant had been declared or not, then it is possible to use PHP's built-in constants function. The name of the constant can be provided to it, as a parameter to this function. The result of this operation will be TRUE if this constant had been declared and FALSE otherwise.

Getting all defined constants in PHP

It is possible to use get_defined_constants method to get a list of all defined constants within PHP code. This result maybe big.

Checking if a PHP constant had been defined

It is possible to check if a PHP constant had been declared by using defined function. Result of this operation will be true if the constant had been declared, and it will be false if not.

PHP class constants

PHP class constants are declared within a class. Scope of these constants are within that class only.